54public class TransportGUI extends JFrame implements ActionListener {
 
   55    private final GridBagConstraints constraints;
 
   56    private final TransportTableModel capacityTableModel, demandTableModel, distanceTableModel, shipmentTableModel;
 
   57    private final JTable capacityTable, demandTable, distanceTable, shipmentTable;
 
   58    private final JTextArea logTextArea;
 
   59    private final JScrollPane logScrollPanel;
 
   60    private final JTextField freightCost, transportCost;
 
   61    private final JButton loadButton, saveButton, runButton, exitButton;
 
   62    private final JFileChooser fc  = 
new JFileChooser();
 
   66    private PrintStream printStream;
 
   68    public static void main(String[] args) {
 
   69        SwingUtilities.invokeLater(
new Runnable() {
 
   77        super(
"Transport - GAMS Java API Example");
 
   78        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
   82        File workingDirectory = 
new File(System.getProperty(
"user.dir"), 
"TransportGUI");
 
   83        workingDirectory.mkdir();
 
   93        JPanel dataPane = 
new JPanel();
 
   94        dataPane.setLayout(
new GridBagLayout());
 
   95        dataPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 
   96        dataPane.setBorder(BorderFactory.createTitledBorder(
"Data"));
 
   98        getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
 
   99        getContentPane().add(Box.createHorizontalGlue());
 
  100        getContentPane().add(dataPane);
 
  101        getContentPane().add(Box.createHorizontalGlue());
 
  103        constraints = 
new GridBagConstraints();
 
  104        constraints.insets = 
new Insets(5, 10, 5, 10);
 
  106        JLabel capacityLabel = 
new JLabel(
"Capacity:");
 
  107        capacityLabel.setToolTipText(
"capacity of plant");
 
  108        constraints.anchor = GridBagConstraints.WEST;
 
  109        constraints.gridx = 0; 
 
  110        constraints.gridy = 0;
 
  111        dataPane.add(capacityLabel, constraints);
 
  113        capacityTableModel = 
new TransportTableModel(inDB.
getParameter(
"a"), capacityLabel.getText());
 
  114        capacityTable = 
new JTable( capacityTableModel );
 
  115        capacityTable.setPreferredScrollableViewportSize(capacityTable.getPreferredSize());
 
  116        capacityTable.setFillsViewportHeight(
true);
 
  117        capacityTable.setAlignmentX(Component.RIGHT_ALIGNMENT);
 
  118        JScrollPane capacityScrollPane = 
new JScrollPane(capacityTable);
 
  119        capacityScrollPane.setMinimumSize(capacityTable.getPreferredSize());
 
  120        capacityScrollPane.setAutoscrolls(
true);
 
  121        constraints.anchor = GridBagConstraints.WEST;
 
  122        constraints.gridx = 0; 
 
  123        constraints.gridy = 1;
 
  124        dataPane.add(capacityScrollPane, constraints);
 
  126        NumberCellRenderer renderer = 
new NumberCellRenderer() ;
 
  127        renderer.setHorizontalAlignment(JLabel.RIGHT);
 
  128        TableColumnModel columnModel = capacityTable.getColumnModel();
 
  129        for(
int i=0; i<2; i++)
 
  130            columnModel.getColumn(i).setCellRenderer(renderer);
 
  132        JLabel demandLabel = 
new JLabel(
"Demand:");
 
  133        demandLabel.setToolTipText(
"demand at market");
 
  134        constraints.anchor = GridBagConstraints.WEST;
 
  135        constraints.gridx = 0; 
 
  136        constraints.gridy = 2;
 
  137        dataPane.add(demandLabel, constraints);
 
  139        demandTableModel = 
new TransportTableModel(inDB.
getParameter(
"b"), demandLabel.getText());
 
  140        demandTable = 
new JTable(demandTableModel);
 
  141        demandTable.setPreferredScrollableViewportSize(demandTable.getPreferredSize());
 
  142        demandTable.setFillsViewportHeight(
true);
 
  143        JScrollPane demandScrollPane = 
new JScrollPane(demandTable);
 
  144        demandScrollPane.setMinimumSize(capacityTable.getPreferredSize());
 
  145        demandScrollPane.setAutoscrolls(
true);
 
  146        constraints.anchor = GridBagConstraints.WEST;
 
  147        constraints.gridx = 0; 
 
  148        constraints.gridy = 3;
 
  149        dataPane.add(demandScrollPane, constraints);
 
  151        columnModel = demandTable.getColumnModel();
 
  152        for(
int i=0; i<3; i++)
 
  153          columnModel.getColumn(i).setCellRenderer(renderer);
 
  155        JLabel distanceLabel = 
new JLabel(
"Distance:");
 
  156        distanceLabel.setToolTipText(
"distance in thousands of miles ");
 
  157        constraints.anchor = GridBagConstraints.WEST; 
 
  158        constraints.gridx = 0; 
 
  159        constraints.gridy = 4; 
 
  160        dataPane.add(distanceLabel, constraints);
 
  162        distanceTableModel = 
new TransportTableModel(inDB.
getParameter(
"d"), distanceLabel.getText());
 
  163        distanceTable = 
new JTable(distanceTableModel);
 
  164        distanceTable.setPreferredScrollableViewportSize(distanceTable.getPreferredSize());
 
  165        distanceTable.setFillsViewportHeight(
true);
 
  166        JScrollPane distanceScrollPane = 
new JScrollPane(distanceTable);
 
  167        distanceScrollPane.setMinimumSize(distanceTable.getPreferredSize());
 
  168        distanceScrollPane.setAutoscrolls(
true);
 
  169        constraints.anchor = GridBagConstraints.WEST;
 
  170        constraints.gridx = 0; 
 
  171        constraints.gridy = 5;
 
  172        dataPane.add(distanceScrollPane, constraints);
 
  174        columnModel = distanceTable.getColumnModel();
 
  175        columnModel.getColumn(2).setCellRenderer(renderer);
 
  177        JLabel costLabel = 
new JLabel(
"Freight Cost:");
 
  178        costLabel.setToolTipText(
"freight in dollars per case per thousand miles");
 
  179        constraints.anchor = GridBagConstraints.WEST; 
 
  180        constraints.gridx = 0; 
 
  181        constraints.gridy = 6; 
 
  182        dataPane.add(costLabel, constraints);
 
  184        freightCost = 
new JTextField(8);
 
  185        freightCost.setHorizontalAlignment(SwingConstants.RIGHT);
 
  186        freightCost.setText(Double.toString(inDB.
getParameter(
"f").getFirstRecord().getValue() ));
 
  187        freightCost.setPreferredSize(freightCost.getPreferredSize());
 
  188        constraints.anchor = GridBagConstraints.WEST; 
 
  189        constraints.fill = GridBagConstraints.HORIZONTAL;
 
  190        constraints.gridx = 0; 
 
  191        constraints.gridy = 7; 
 
  192        dataPane.add(freightCost, constraints);
 
  195        logTextArea = 
new JTextArea(15, 50);
 
  196        logTextArea.setLineWrap(
true);
 
  197        logTextArea.setWrapStyleWord(
true);
 
  198        logTextArea.setEditable(
false);
 
  199        Font font = 
new Font( 
"Monospaced", Font.PLAIN, 12 );
 
  200        logTextArea.setFont( font );
 
  203        printStream = 
new PrintStream(
new LogOutputStream(logTextArea));
 
  204        System.setOut(printStream);
 
  205        System.setErr(printStream);
 
  207        logScrollPanel = 
new JScrollPane(logTextArea); 
 
  208        logScrollPanel.setMinimumSize(logTextArea.getPreferredSize());
 
  209        logScrollPanel.setAutoscrolls(
true);
 
  211        JPanel runPane = 
new JPanel();
 
  212        runPane.setLayout(
new GridBagLayout());
 
  213        runPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 
  214        runPane.setBorder(
new EmptyBorder(5,5,5,5));
 
  216        loadButton = makeButton(
"Load Data");
 
  217        loadButton.setToolTipText(
"Load Data from gdx file");
 
  218        constraints.anchor = GridBagConstraints.CENTER;
 
  219        constraints.fill = GridBagConstraints.HORIZONTAL;
 
  220        constraints.gridx = 0;
 
  221        constraints.gridy = 0;
 
  222        runPane.add(loadButton, constraints);
 
  224        saveButton = makeButton(
"Save Data");
 
  225        saveButton.setToolTipText(
"Save Data to gdx file");
 
  226        constraints.gridx = 1;
 
  227        runPane.add(saveButton, constraints);
 
  229        runButton = makeButton(
"Run");
 
  230        runButton.setToolTipText(
"Run Transport Model");
 
  231        constraints.gridx = 2;
 
  232        runPane.add(runButton, constraints);
 
  234        exitButton = makeButton(
"Exit");
 
  235        runButton.setToolTipText(
"Exit Transport Example");
 
  236        constraints.gridx = 3;
 
  237        runPane.add(exitButton, constraints);
 
  238        getContentPane().add(runPane);
 
  241        JPanel outputPane = 
new JPanel();
 
  242        outputPane.setBorder(BorderFactory.createTitledBorder(
"Output"));
 
  243        getContentPane().add(outputPane);
 
  245        JTabbedPane resultPane = 
new JTabbedPane();
 
  247        JPanel resultInnerPane = 
new JPanel();
 
  248        resultInnerPane.setLayout(
new GridBagLayout());
 
  249        resultInnerPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 
  251        JLabel transportCostLabel = 
new JLabel(
"Transportation Cost:");
 
  252        transportCostLabel.setToolTipText(
"total transportation costs in thousands of dollars");
 
  253        constraints.anchor = GridBagConstraints.WEST;
 
  254        constraints.gridx = 0; 
 
  255        constraints.gridy = 1;
 
  256        resultInnerPane.add(transportCostLabel, constraints);
 
  258        transportCost = 
new JTextField(12);
 
  259        transportCost.setHorizontalAlignment(SwingConstants.RIGHT);
 
  260        transportCost.setText(
"");
 
  261        constraints.anchor = GridBagConstraints.WEST; 
 
  262        constraints.gridx = 0; 
 
  263        constraints.gridy = 2; 
 
  264        resultInnerPane.add(transportCost, constraints);
 
  266        JLabel shipmentLabel = 
new JLabel(
"Shipment:");
 
  267        shipmentLabel.setToolTipText(
"shipment quantities");
 
  268        constraints.anchor = GridBagConstraints.WEST;
 
  269        constraints.gridx = 0; 
 
  270        constraints.gridy = 3;
 
  271        resultInnerPane.add(shipmentLabel, constraints);
 
  273        shipmentTableModel = 
new TransportTableModel((
GAMSVariable)
null, shipmentLabel.getText());
 
  274        shipmentTable = 
new JTable(shipmentTableModel);
 
  275        shipmentTable.setPreferredScrollableViewportSize(shipmentTable.getPreferredSize());
 
  276        shipmentTable.setFillsViewportHeight(
true);
 
  277        JScrollPane shipmentScrollPane = 
new JScrollPane(shipmentTable);
 
  278        constraints.anchor = GridBagConstraints.WEST;
 
  279        constraints.gridx = 0; 
 
  280        constraints.gridy = 4;
 
  281        resultInnerPane.add(shipmentScrollPane, constraints);
 
  283        columnModel = shipmentTable.getColumnModel();
 
  284        for(
int i=2; i<4; i++)
 
  285          columnModel.getColumn(i).setCellRenderer(renderer);
 
  287        resultPane.setAutoscrolls(
true);
 
  288        resultPane.addTab(
"Results", resultInnerPane);
 
  289        resultPane.addTab(
"log",logScrollPanel);
 
  291        outputPane.add(resultPane);
 
  292        outputPane.add(Box.createHorizontalGlue());
 
  300    private JButton makeButton(String caption) {
 
  301        JButton b = 
new JButton(caption);
 
  302        b.setActionCommand(caption);
 
  303        b.addActionListener(
this);
 
  304        getContentPane().add(b, constraints);
 
  311        capacity.findRecord(
"seattle").setValue( 
 
  312            (capacityTable.getValueAt(0,0) instanceof Double) ? ((Double)capacityTable.getValueAt(0,0)).doubleValue() :  Double.parseDouble((String)capacityTable.getValueAt(0,0))
 
  314        capacity.findRecord(
"san-diego").setValue( 
 
  315                (capacityTable.getValueAt(0,1) instanceof Double) ? ((Double)capacityTable.getValueAt(0,1)).doubleValue() :  Double.parseDouble((String)capacityTable.getValueAt(0,1))
 
  318        demand.findRecord(
"new-york").setValue( 
 
  319            (demandTable.getValueAt(0,0) instanceof Double) ? ((Double)demandTable.getValueAt(0,0)).doubleValue() :  Double.parseDouble((String)demandTable.getValueAt(0,0))
 
  321        demand.findRecord(
"chicago").setValue( 
 
  322            (demandTable.getValueAt(0,1) instanceof Double) ? ((Double)demandTable.getValueAt(0,1)).doubleValue() :  Double.parseDouble((String)demandTable.getValueAt(0,1))
 
  324        demand.findRecord(
"topeka").setValue( 
 
  325            (demandTable.getValueAt(0,2) instanceof Double) ? ((Double)demandTable.getValueAt(0,2)).doubleValue() :  Double.parseDouble((String)demandTable.getValueAt(0,2))
 
  328        distance.findRecord(
"seattle", 
"new-york").setValue( 
 
  329            (distanceTable.getValueAt(0,2) instanceof Double) ? ((Double)distanceTable.getValueAt(0,2)).doubleValue() :  Double.parseDouble((String)distanceTable.getValueAt(0,2))
 
  331        distance.findRecord(
"seattle", 
"chicago").setValue( 
 
  332            (distanceTable.getValueAt(1,2) instanceof Double) ? ((Double)distanceTable.getValueAt(1,2)).doubleValue() :  Double.parseDouble((String)distanceTable.getValueAt(1,2))
 
  334        distance.findRecord(
"seattle", 
"topeka").setValue( 
 
  335            (distanceTable.getValueAt(2,2) instanceof Double) ? ((Double)distanceTable.getValueAt(2,2)).doubleValue() :  Double.parseDouble((String)distanceTable.getValueAt(2,2))
 
  337        distance.findRecord(
"san-diego", 
"new-york").setValue( 
 
  338            (distanceTable.getValueAt(3,2) instanceof Double) ? ((Double)distanceTable.getValueAt(3,2)).doubleValue() :  Double.parseDouble((String)distanceTable.getValueAt(3,2))
 
  340        distance.findRecord(
"san-diego", 
"chicago").setValue( 
 
  341            (distanceTable.getValueAt(4,2) instanceof Double) ? ((Double)distanceTable.getValueAt(4,2)).doubleValue() :  Double.parseDouble((String)distanceTable.getValueAt(4,2))
 
  343        distance.findRecord(
"san-diego", 
"topeka").setValue( 
 
  344            (distanceTable.getValueAt(5,2) instanceof Double) ? ((Double)distanceTable.getValueAt(5,2)).doubleValue() :  Double.parseDouble((String)distanceTable.getValueAt(5,2))
 
  347        f.getFirstRecord().setValue( Double.parseDouble(freightCost.getText()) );
 
  351    public void actionPerformed(ActionEvent event) {
 
  352        if (event.getSource() == runButton) {
 
  353            runButton.setEnabled(
false);
 
  359                job.
run(opt, printStream, inDB);
 
  360            } 
catch(Exception e) { 
 
  363                runButton.setEnabled(
true);
 
  364                if (job.
OutDB() != 
null) {
 
  365                   double roundedValue = Math.round(job.
OutDB().
getVariable(
"z").getFirstRecord().getLevel() * 100D) / 100D;
 
  366                   transportCost.setText( Double.toString( roundedValue ));
 
  369                       shipmentTableModel.setValueAt(rec.getKey(0), i, 0);
 
  370                       shipmentTableModel.setValueAt(rec.getKey(1), i, 1);
 
  371                       shipmentTableModel.setValueAt(
new Double(rec.getLevel()), i, 2);
 
  372                       shipmentTableModel.setValueAt(
new Double(rec.getMarginal()), i, 3);
 
  377        } 
else if (event.getSource() == loadButton) {
 
  378            runButton.setEnabled(
true);
 
  379            FileNameExtensionFilter gdxfilter = 
new FileNameExtensionFilter( 
"GAMS Data eXchange files (*.gdx)", 
"gdx");
 
  380            fc.setDialogTitle(
"Load data from .gdx file");
 
  381            fc.setFileFilter(gdxfilter);
 
  382            int returnVal = fc.showOpenDialog(
this);
 
  383            if (returnVal == JFileChooser.APPROVE_OPTION) {
 
  384                File file = fc.getSelectedFile();
 
  389                       capacityTableModel.setValueAt(
new Double(rec.getValue()), 0, j++);
 
  393                       demandTableModel.setValueAt(
new Double(rec.getValue()), 0, j++);
 
  397                       distanceTableModel.setValueAt(rec.getKey(0), i, 0);
 
  398                       distanceTableModel.setValueAt(rec.getKey(1), i, 1);
 
  399                       distanceTableModel.setValueAt(
new Double(rec.getValue()), i, 2);
 
  403                    transportCost.setText(
"");
 
  405                       shipmentTableModel.setValueAt(
"", i, 0);
 
  406                       shipmentTableModel.setValueAt(
"", i, 1);
 
  407                       shipmentTableModel.setValueAt(
"", i, 2);
 
  408                       shipmentTableModel.setValueAt(
"", i, 3);
 
  410                    System.out.println(
"--- Data loaded from [" + file.getAbsolutePath() + 
"].\n");
 
  412                    System.out.println(
"--- Data could not be loaded from [" + file.getAbsolutePath() + 
"]");
 
  416               System.out.println(
"--- Load Data command cancelled by user.\n");
 
  418        } 
else if (event.getSource() == saveButton) {
 
  419            runButton.setEnabled(
true);
 
  420            FileNameExtensionFilter gdxfilter = 
new FileNameExtensionFilter( 
"GAMS Data eXchange files (*.gdx)", 
"gdx");
 
  421            fc.setFileFilter(gdxfilter);
 
  422            fc.setDialogTitle(
"Save Data to .gdx file");
 
  423            int returnVal = fc.showSaveDialog(
null);
 
  424            if (returnVal == JFileChooser.APPROVE_OPTION) {
 
  425                File file = fc.getSelectedFile();
 
  428                    inDB.
export(file.getAbsolutePath());
 
  429                    System.out.println(
"--- Data saved to ["+file.getAbsolutePath()+
"].\n");
 
  431                     System.out.println(
"--- Data could not be saved to [" + file.getAbsolutePath() + 
"]\n");
 
  435                System.out.println(
"--- Save Data command cancelled by user.\n");
 
  437        } 
else if (event.getSource() == exitButton) {
 
  438            System.out.println(
"--- Closing Transport GUI Example.\n");
 
  445            "  i   canning plants   / seattle, san-diego /                    \n" +
 
  446            "  j   markets          / new-york, chicago, topeka / ;           \n" +
 
  449            "  a(i)  capacity of plant i in cases                             \n" +
 
  450            "                     /    seattle     350                        \n" +
 
  451            "                          san-diego   600  /                     \n" +
 
  453            "  b(j)  demand at market j in cases                              \n" +
 
  454            "                     /    new-york    325                        \n" +
 
  456            "                          topeka      275  / ;                   \n" +
 
  458            "Table d(i,j)  distance in thousands of miles                     \n" +
 
  459            "               new-york       chicago      topeka                \n" +
 
  460            "  seattle          2.5           1.7          1.8                \n" +
 
  461            "  san-diego        2.5           1.8          1.4  ;             \n" +
 
  463            "Scalar f  freight in dollars per case per thousand miles  /90/ ; \n ";
 
  465    static String model = 
"Sets                                                         \n" +
 
  466            "      i   canning plants                                                   \n" +
 
  470            "      a(i)   capacity of plant i in cases                                  \n" +
 
  471            "      b(j)   demand at market j in cases                                   \n" +
 
  472            "      d(i,j) distance in thousands of miles                                \n" +
 
  473            " Scalar f  freight in dollars per case per thousand miles;                 \n" +
 
  475            "$if not set gdxincname $abort 'no include file name for data file provided'\n" +
 
  476            "$gdxin %gdxincname%                                                        \n" +
 
  477            "$load i j a b d f                                                          \n" +
 
  480            " Parameter c(i,j)  transport cost in thousands of dollars per case ;       \n" +
 
  482            "            c(i,j) = f * d(i,j) / 1000 ;                                   \n" +
 
  485            "       x(i,j)  shipment quantities in cases                                \n" +
 
  486            "       z       total transportation costs in thousands of dollars ;        \n" +
 
  488            " Positive Variable x ;                                                     \n" +
 
  492            "      cost        define objective function                                \n" +
 
  493            "      supply(i)   observe supply limit at plant i                          \n" +
 
  494            "      demand(j)   satisfy demand at market j ;                             \n" +
 
  496            "  cost ..        z  =e=  sum((i,j), c(i,j)*x(i,j)) ;                       \n" +
 
  498            "  supply(i) ..   sum(j, x(i,j))  =l=  a(i) ;                               \n" +
 
  500            "  demand(j) ..   sum(i, x(i,j))  =g=  b(j) ;                               \n" +
 
  502            " Model transport /all/ ;                                                   \n" +
 
  504            " Solve transport using lp minimizing z ;                                   \n" +
 
  506            " Display x.l, x.m ;                                                        \n" +