~cpn-gui/tapaal/cpn-editor

« back to all changes in this revision

Viewing changes to src/pipe/gui/widgets/ColoredArcDialogPanel.java

  • Committer: Mark Glavind
  • Date: 2019-02-28 13:31:29 UTC
  • mfrom: (1015.1.10 arcColorTime)
  • Revision ID: mglavi14@student.aau.dk-20190228133129-q1imqiutj1iadgda
merge from arcColorTime, feature finished

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
import dk.aau.cs.debug.Logger;
4
4
import dk.aau.cs.gui.Context;
5
 
import dk.aau.cs.model.tapn.Colored.ColorType;
6
 
import dk.aau.cs.model.tapn.Colored.ColoredInputArc;
7
 
import dk.aau.cs.model.tapn.Colored.ColoredOutputArc;
 
5
import dk.aau.cs.model.tapn.Bound;
 
6
import dk.aau.cs.model.tapn.Colored.*;
 
7
import dk.aau.cs.model.tapn.Colored.Color;
8
8
import dk.aau.cs.model.tapn.Colored.ExpressionSupport.ExprStringPosition;
9
9
import dk.aau.cs.model.tapn.Colored.Expressions.*;
10
 
import dk.aau.cs.model.tapn.TimedPlace;
 
10
import dk.aau.cs.model.tapn.RatBound;
 
11
import dk.aau.cs.model.tapn.TimeInterval;
11
12
import pipe.gui.CreateGui;
12
13
import pipe.gui.graphicElements.PetriNetObject;
13
14
import pipe.gui.graphicElements.cpn.ColoredInputArcComponent;
14
15
import pipe.gui.graphicElements.cpn.ColoredOutputArcComponent;
15
 
import pipe.gui.graphicElements.tapn.TimedInputArcComponent;
16
16
 
17
17
import javax.swing.*;
18
18
import javax.swing.event.DocumentEvent;
23
23
import javax.swing.text.StyledDocument;
24
24
import java.awt.*;
25
25
import java.awt.event.*;
 
26
import java.math.BigDecimal;
26
27
import java.util.ArrayList;
 
28
import java.util.List;
27
29
import java.util.Vector;
28
30
 
29
31
public class ColoredArcDialogPanel extends GuardDialogue {
47
49
    private JButton addColorExpressionButton;
48
50
    private JSpinner scalarJSpinner;
49
51
 
 
52
    private JPanel timedGuardColorPanel;
 
53
    private JComboBox<ColoredTimeInterval> activeTimeGuardColorsComboBox;
 
54
    private JLabel activeTimeGuardsLabel;
 
55
    private JButton removeActiveColorButton;
 
56
    private JComboBox<ColoredTimeInterval> inactiveTimedGuardColorsCombobox;
 
57
    private JButton addInactiveColorButton;
 
58
    private JLabel inactiveTimeGuardsLabel;
 
59
 
50
60
    private JPanel editPanel;
51
61
    private ButtonGroup editButtonsGroup;
52
62
    private JButton resetExprButton;
58
68
    private ColoredInputArcComponent objectToBeEditedInput;
59
69
    private ColoredOutputArcComponent objectToBeEditedOutput;
60
70
    private boolean input;
61
 
    private TimedInputArcComponent test;
62
71
    private Context context;
63
 
    private ArrayList<TimedPlace> places;
64
72
    private ArcExpression arcExpression;
65
73
    private ExprStringPosition currentSelection = null;
66
 
 
67
74
    private PetriNetObject petriNetObject;
 
75
    private ColoredTimeInterval activeTimeGuard;
68
76
 
69
77
    public ColoredArcDialogPanel(JRootPane rootPane, PetriNetObject objectToBeEdited, Context context) {
70
78
        super(rootPane, objectToBeEdited);
88
96
        initPanels();
89
97
        initExpr();
90
98
        initonOK();
 
99
        if (input)
 
100
            initializeColoredTimedGuard();
 
101
    }
 
102
 
 
103
    private void initializeColoredTimedGuard() { // we get all available colors from the color type of the connecting place and add them to inactive colors
 
104
                                                // Only if there are no prior invariants set for this arc
 
105
 
 
106
        List<ColoredTimeInterval> coloredTimeIntervalList;
 
107
        coloredTimeIntervalList = ((ColoredInputArc)objectToBeEditedInput.underlyingTimedInputArc()).getColorTimeIntervals();
 
108
 
 
109
        for (ColoredTimeInterval timeInterval : coloredTimeIntervalList) {
 
110
            if (timeInterval.isActive()) {
 
111
                activeTimeGuardColorsComboBox.addItem(timeInterval);
 
112
            }
 
113
            else {
 
114
                inactiveTimedGuardColorsCombobox.addItem(timeInterval);
 
115
            }
 
116
 
 
117
        }
 
118
        activeTimeGuard = (ColoredTimeInterval) activeTimeGuardColorsComboBox.getSelectedItem();
91
119
    }
92
120
 
93
121
    private void initExpr() {
269
297
        }
270
298
    }
271
299
 
 
300
    private void updateOldTimeInterval() { // we save the changes made in the gui to the time interval object associated with the color
 
301
        boolean isLowerIncluded;
 
302
        boolean isUpperIncluded;
 
303
        int firstValue = Integer.parseInt(firstIntervalNumber.getValue().toString());
 
304
        int secondValue = Integer.parseInt(secondIntervalNumber.getValue().toString());
 
305
        Bound lower = new RatBound(BigDecimal.valueOf(firstValue));
 
306
        Bound upper;
 
307
 
 
308
        if (inf.isSelected()) {
 
309
            upper = Bound.Infinity;
 
310
        } else {
 
311
            upper = new RatBound(BigDecimal.valueOf(secondValue));
 
312
        }
 
313
        if (leftDelimiter.getSelectedItem().equals("["))
 
314
            isLowerIncluded = true;
 
315
        else
 
316
            isLowerIncluded = false;
 
317
 
 
318
        if (rightDelimiter.getSelectedItem().equals("]"))
 
319
            isUpperIncluded = true;
 
320
        else
 
321
            isUpperIncluded = false;
 
322
 
 
323
        for (int i = 0; i < activeTimeGuardColorsComboBox.getItemCount(); i++) {
 
324
            if (activeTimeGuardColorsComboBox.getItemAt(i).equals(activeTimeGuard)) {
 
325
                activeTimeGuardColorsComboBox.getItemAt(i).setLowerBound(lower);
 
326
                activeTimeGuardColorsComboBox.getItemAt(i).setUpperBound(upper);
 
327
                activeTimeGuardColorsComboBox.getItemAt(i).setIsLowerIncluded(isLowerIncluded);
 
328
                activeTimeGuardColorsComboBox.getItemAt(i).setIsUpperIncluded(isUpperIncluded);
 
329
                break;
 
330
            }
 
331
        }
 
332
    }
 
333
 
 
334
    private void setNewTimeInterval() { // we update the gui for time intervals to reflect the time interval object assoicated with the new color selected
 
335
        ColoredTimeInterval activeInterval = activeTimeGuardColorsComboBox.getItemAt(activeTimeGuardColorsComboBox.getSelectedIndex());
 
336
        activeTimeGuard = activeInterval;
 
337
 
 
338
        if (activeInterval != null) {
 
339
            int lower = activeInterval.lowerBound().value();
 
340
            int upper = activeInterval.upperBound().value();
 
341
            boolean isUpperIncl = activeInterval.isUpperIncluded();
 
342
            boolean isLowerIncl = activeInterval.isLowerIncluded();
 
343
 
 
344
            SpinnerNumberModel firstNumberModel = new SpinnerNumberModel(lower, 0, Integer.MAX_VALUE, 1);
 
345
            SpinnerNumberModel secondNumberModel = null;
 
346
            if (upper == -1) { // -1 is equal to infinity bound
 
347
                inf.setSelected(true);
 
348
                secondIntervalNumber.setEnabled(false);
 
349
                rightDelimiter.setEnabled(false);
 
350
                secondNumberModel = new SpinnerNumberModel(0, 0, Integer.MAX_VALUE, 1);
 
351
            } else {
 
352
                inf.setSelected(false);
 
353
                secondIntervalNumber.setEnabled(true);
 
354
                rightDelimiter.setEnabled(true);
 
355
                secondNumberModel = new SpinnerNumberModel(upper, 0, Integer.MAX_VALUE, 1);
 
356
            }
 
357
 
 
358
            firstIntervalNumber.setModel(firstNumberModel);
 
359
            secondIntervalNumber.setModel(secondNumberModel);
 
360
 
 
361
            //TODO: Might be missing case where upper and lower are not numbers, but constants instead. see setNonColoredInitialState in GuardDialogue
 
362
 
 
363
            if (isLowerIncl)
 
364
                leftDelimiter.setSelectedItem("[");
 
365
            else
 
366
                leftDelimiter.setSelectedItem("(");
 
367
 
 
368
            if (isUpperIncl)
 
369
                rightDelimiter.setSelectedItem("]");
 
370
            else
 
371
                rightDelimiter.setSelectedItem(")");
 
372
 
 
373
        }
 
374
    }
 
375
 
 
376
 
272
377
    private void initonOK() {
273
378
        okButton.addActionListener(new ActionListener() {
274
379
            @Override
275
380
            public void actionPerformed(ActionEvent actionEvent) {
276
381
                if (input) {
277
382
                    onOkColored();
278
 
                    onOK(objectToBeEditedInput);
 
383
                   // onOK(objectToBeEditedInput);
279
384
                } else {
280
385
                    onOkColored();
281
 
                    onOK(objectToBeEditedOutput);
 
386
                  //  onOK(objectToBeEditedOutput);
282
387
                }
283
388
            }
284
389
        });
285
390
    }
286
391
 
 
392
    private List<ColoredTimeInterval> getCTIList() {
 
393
        List<ColoredTimeInterval> ctiList = new ArrayList<ColoredTimeInterval>();
 
394
 
 
395
        for (int i = 0; i < activeTimeGuardColorsComboBox.getItemCount(); i++) {
 
396
            ctiList.add(activeTimeGuardColorsComboBox.getItemAt(i));
 
397
        }
 
398
        for (int i = 0; i < inactiveTimedGuardColorsCombobox.getItemCount(); i++) {
 
399
            ctiList.add(inactiveTimedGuardColorsCombobox.getItemAt(i));
 
400
        }
 
401
 
 
402
        return ctiList;
 
403
    }
 
404
 
287
405
    private void onOkColored() {
 
406
        if (input)
 
407
            updateOldTimeInterval();
288
408
        if (input) {
289
409
            ColoredInputArc inputArc = (ColoredInputArc)objectToBeEditedInput.underlyingTimedInputArc();
290
410
            inputArc.setExpression(arcExpression);
291
411
            objectToBeEditedInput.setUnderlyingArc(inputArc);
292
412
            ((ColoredInputArc)((ColoredInputArcComponent)petriNetObject).underlyingTimedInputArc()).setExpression(arcExpression);
 
413
            ((ColoredInputArc)((ColoredInputArcComponent)petriNetObject).underlyingTimedInputArc()).setColorTimeIntervals(getCTIList());
293
414
        } else
294
415
        {
295
416
            ColoredOutputArc outputArc = (ColoredOutputArc)objectToBeEditedOutput.underlyingArc();
296
417
            objectToBeEditedOutput.setUnderlyingArc(outputArc);
297
418
            ((ColoredOutputArc)((ColoredOutputArcComponent)petriNetObject).underlyingArc()).setExpression(arcExpression);
298
 
 
299
419
        }
300
420
    }
301
421
 
302
422
    private void initPanels() {
303
423
        exprPanel = new JPanel(new GridBagLayout());
304
424
        exprPanel.setBorder(BorderFactory.createTitledBorder("Arc Expressions"));
 
425
        if (input)
 
426
            initColoredTimedGuard();
305
427
 
306
428
        initExprField();
307
429
        initNumberExpressionsPanel();
673
795
        exprPanel.add(numberExprPanel, gbc);
674
796
    }
675
797
 
 
798
    private void initColoredTimedGuard() {
 
799
        timedGuardColorPanel = new JPanel(new GridBagLayout());
 
800
        timedGuardColorPanel.setBorder(BorderFactory.createTitledBorder("Colors for Timed Guard"));
 
801
 
 
802
        activeTimeGuardColorsComboBox = new JComboBox<ColoredTimeInterval>();
 
803
        activeTimeGuardsLabel = new JLabel("Active time guards:");
 
804
        removeActiveColorButton = new JButton("Remove time guard");
 
805
 
 
806
        inactiveTimedGuardColorsCombobox = new JComboBox<ColoredTimeInterval>();
 
807
        inactiveTimeGuardsLabel = new JLabel("Inactive time guards:");
 
808
        addInactiveColorButton = new JButton("Add time guard");
 
809
 
 
810
 
 
811
        Dimension dimComboBox = new Dimension(175, 25);
 
812
        Dimension dimButton = new Dimension(125, 25);
 
813
 
 
814
        activeTimeGuardColorsComboBox.setPreferredSize(dimComboBox);
 
815
        inactiveTimedGuardColorsCombobox.setPreferredSize(dimComboBox);
 
816
        addInactiveColorButton.setPreferredSize(dimButton);
 
817
        removeActiveColorButton.setPreferredSize(dimButton);
 
818
 
 
819
        addInactiveColorButton.addActionListener(new ActionListener() {
 
820
            @Override
 
821
            public void actionPerformed(ActionEvent actionEvent) {
 
822
                ColoredTimeInterval selectedTimeInterval = inactiveTimedGuardColorsCombobox.getItemAt(inactiveTimedGuardColorsCombobox.getSelectedIndex());
 
823
                selectedTimeInterval.setActive(true);
 
824
                activeTimeGuardColorsComboBox.addItem(selectedTimeInterval);
 
825
                inactiveTimedGuardColorsCombobox.removeItemAt(inactiveTimedGuardColorsCombobox.getSelectedIndex());
 
826
                if (inactiveTimedGuardColorsCombobox.getItemCount() < 0) {
 
827
                    inactiveTimedGuardColorsCombobox.setSelectedIndex(0);
 
828
                }
 
829
            }
 
830
        });
 
831
 
 
832
        removeActiveColorButton.addActionListener(new ActionListener() {
 
833
            @Override
 
834
            public void actionPerformed(ActionEvent actionEvent) {
 
835
                ColoredTimeInterval selectedTimeInterval = activeTimeGuardColorsComboBox.getItemAt(activeTimeGuardColorsComboBox.getSelectedIndex());
 
836
                selectedTimeInterval.setActive(false);
 
837
                activeTimeGuardColorsComboBox.removeItemAt(activeTimeGuardColorsComboBox.getSelectedIndex());
 
838
                inactiveTimedGuardColorsCombobox.addItem(selectedTimeInterval);
 
839
            }
 
840
        });
 
841
 
 
842
        activeTimeGuardColorsComboBox.addActionListener(new ActionListener() {
 
843
            @Override
 
844
            public void actionPerformed(ActionEvent actionEvent) {
 
845
               // activeTimeGuardColorsComboBox.setSelectedIndex(activeTimeGuardColorsComboBox.getItemCount()-1);
 
846
 
 
847
                Logger.log(activeTimeGuardColorsComboBox.getItemAt(activeTimeGuardColorsComboBox.getSelectedIndex()).getColor().getColorName());
 
848
                ColoredTimeInterval selectedTimeInterval = activeTimeGuardColorsComboBox.getItemAt(activeTimeGuardColorsComboBox.getSelectedIndex());
 
849
                if (selectedTimeInterval.getColor().getColorName().equals("*")) {
 
850
                    removeActiveColorButton.setEnabled(false);
 
851
                } else {
 
852
                    removeActiveColorButton.setEnabled(true);
 
853
                }
 
854
                 updateOldTimeInterval();
 
855
                 setNewTimeInterval();
 
856
 
 
857
            }
 
858
 
 
859
        });
 
860
 
 
861
        GridBagConstraints gbc = new GridBagConstraints();
 
862
        gbc.gridx = 0;
 
863
        gbc.gridy = 0;
 
864
        gbc.anchor = GridBagConstraints.WEST;
 
865
        timedGuardColorPanel.add(inactiveTimeGuardsLabel, gbc);
 
866
 
 
867
        gbc.gridx = 1;
 
868
        timedGuardColorPanel.add(inactiveTimedGuardColorsCombobox, gbc);
 
869
 
 
870
        gbc.gridy = 1;
 
871
        gbc.anchor = GridBagConstraints.EAST;
 
872
        gbc.insets = new Insets(5, 0, 5, 0);
 
873
        timedGuardColorPanel.add(addInactiveColorButton, gbc);
 
874
 
 
875
 
 
876
        gbc = new GridBagConstraints();
 
877
        gbc.gridx = 2;
 
878
        gbc.gridy = 0;
 
879
        gbc.anchor = GridBagConstraints.WEST;
 
880
        timedGuardColorPanel.add(activeTimeGuardsLabel, gbc);
 
881
 
 
882
        gbc.gridx = 3;
 
883
        timedGuardColorPanel.add(activeTimeGuardColorsComboBox, gbc);
 
884
 
 
885
        gbc.gridy = 1;
 
886
        gbc.insets = new Insets(5, 0, 5, 0);
 
887
        gbc.anchor = GridBagConstraints.EAST;
 
888
        timedGuardColorPanel.add(removeActiveColorButton, gbc);
 
889
 
 
890
        gbc.gridx = 0;
 
891
        gbc.gridy = 0;
 
892
        gbc.insets = new Insets(5, 10, 5, 10);
 
893
        gbc.fill = GridBagConstraints.BOTH;
 
894
        gbc.anchor = GridBagConstraints.WEST;
 
895
        add(timedGuardColorPanel, gbc);
 
896
    }
 
897
 
676
898
    private void initExprField () {
677
899
        exprField = new JTextPane();
678
900