View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.dialogs;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.swing.JComboBox;
22  
23  import p3j.database.DatabaseFactory;
24  import p3j.gui.panels.PropertiesShowPanelFactory;
25  import p3j.gui.panels.projections.IProjectionTree;
26  import p3j.gui.panels.projections.ProjectionTreeNode;
27  import p3j.misc.gui.GUI;
28  import p3j.pppm.parameters.ParameterAssignment;
29  import p3j.pppm.sets.Set;
30  import p3j.pppm.sets.SetType;
31  
32  /**
33   * Dialog to choose a new set for a parameter assignment.
34   * 
35   * @see Set
36   * @see ParameterAssignment
37   * @author Christina Bohk
38   * @author Roland Ewald
39   */
40  public class MoveAssignmentToSetDialog extends ProjectionDialog {
41  
42    /** Serialization ID. */
43    private static final long serialVersionUID = -1158858943985692727L;
44  
45    /** The parameter assignment to be moved. */
46    private final ParameterAssignment assignment;
47  
48    /** The set the parameter assignment is currently associated with. */
49    private final Set oldSet;
50  
51    /** The current Settype. */
52    private final SetType setType;
53  
54    /** The projection tree. */
55    private final IProjectionTree projectionTree;
56  
57    /** The select box to choose the destination set. */
58    private final JComboBox<Set> desitnationSetSelector = new JComboBox<Set>();
59  
60    /**
61     * Instantiates a new move assignment to set dialog.
62     * 
63     * @param parameterAssignment
64     *          the parameter assignment
65     * @param projTree
66     *          the projection tree
67     * @param assignmentSet
68     *          the set the assignment is currently stored in
69     * @param currentSetType
70     *          the current Settype
71     * @param node
72     *          the node of the current set
73     */
74    public MoveAssignmentToSetDialog(ParameterAssignment parameterAssignment,
75        IProjectionTree projTree, Set assignmentSet, SetType currentSetType) {
76      assignment = parameterAssignment;
77      oldSet = assignmentSet;
78      projectionTree = projTree;
79      setType = currentSetType;
80      initUI();
81    }
82  
83    @Override
84    protected void okAction() {
85      Object item = desitnationSetSelector.getSelectedItem();
86      if (!(item instanceof Set)) {
87        return;
88      }
89      Set destSet = (Set) item;
90      oldSet.removeParameterAssignment(assignment);
91      destSet.addParameterAssignment(assignment);
92      DatabaseFactory.getDatabaseSingleton().saveSet(oldSet);
93      DatabaseFactory.getDatabaseSingleton().saveSet(destSet);
94      projectionTree.totalRefresh();
95      ProjectionTreeNode<?> selectNode = ((ProjectionTreeNode<?>) projectionTree
96          .getTreeModel().getRoot()).getChildWithEntity(assignment
97          .getParamInstance());
98      if (selectNode.getChildCount() > 0) {
99        selectNode = (ProjectionTreeNode<?>) selectNode.getChildAt(0);
100     }
101     projectionTree.selectNode(selectNode);
102     setVisible(false);
103   }
104 
105   /**
106    * Initialize the user interface.
107    */
108   private void initUI() {
109     setTitle("Moving a parameter assignment");
110     setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
111     setModal(true);
112     GUI.centerOnScreen(this);
113 
114     List<Set> sets = new ArrayList<Set>(setType.getSets());
115     sets.remove(oldSet);
116 
117     for (Set s : sets) {
118       desitnationSetSelector.addItem(s);
119     }
120 
121     PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
122         getButtons(), 1);
123     pspf.sep("Assignment '" + assignment.getName() + "'");
124     pspf.app("Move to set:", desitnationSetSelector);
125     getContentPane().add(pspf.constructPanel());
126   }
127 
128   /**
129    * Checks if showing the dialog is meaningful.
130    * 
131    * @return true, if showing is meaningful because there is at least one other
132    *         set
133    */
134   public boolean isMeaningful() {
135     return desitnationSetSelector.getItemCount() > 0;
136   }
137 }