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.awt.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import javax.swing.JComboBox;
24  
25  import p3j.database.DatabaseFactory;
26  import p3j.gui.panels.PropertiesShowPanelFactory;
27  import p3j.gui.panels.projections.IProjectionTree;
28  import p3j.gui.panels.projections.ProjectionTreeNode;
29  import p3j.misc.gui.GUI;
30  import p3j.pppm.parameters.ParameterAssignment;
31  import p3j.pppm.sets.Set;
32  import p3j.pppm.sets.SetType;
33  
34  /**
35   * Dialog to choose a new set for a parameter assignment.
36   * 
37   * @see Set
38   * @see ParameterAssignment
39   * @author Christina Bohk
40   * @author Roland Ewald
41   */
42  public class MoveAssignmentToSetDialog extends ProjectionDialog {
43  
44  	/**
45  	 * The listener interface for clicks on the OK button.
46  	 */
47  	private final class OKActionListener implements ActionListener {
48  
49  		@Override
50  		public void actionPerformed(ActionEvent e) {
51  			Object item = desitnationSetSelector.getSelectedItem();
52  			if (!(item instanceof Set)) {
53  				return;
54  			}
55  			Set destSet = (Set) item;
56  			oldSet.removeParameterAssignment(assignment);
57  			destSet.addParameterAssignment(assignment);
58  			DatabaseFactory.getDatabaseSingleton().saveSet(oldSet);
59  			DatabaseFactory.getDatabaseSingleton().saveSet(destSet);
60  			projectionTree.totalRefresh();
61  			ProjectionTreeNode<?> selectNode = ((ProjectionTreeNode<?>) projectionTree
62  			    .getTreeModel().getRoot()).getChildWithEntity(assignment
63  			    .getParamInstance());
64  			if (selectNode.getChildCount() > 0) {
65  				selectNode = (ProjectionTreeNode<?>) selectNode.getChildAt(0);
66  			}
67  			projectionTree.selectNode(selectNode);
68  			setVisible(false);
69  		}
70  	}
71  
72  	/** Serialization ID. */
73  	private static final long serialVersionUID = -1158858943985692727L;
74  
75  	/** The parameter assignment to be moved. */
76  	private final ParameterAssignment assignment;
77  
78  	/** The set the parameter assignment is currently associated with. */
79  	private final Set oldSet;
80  
81  	/** The current Settype. */
82  	private final SetType setType;
83  
84  	/** The projection tree. */
85  	private final IProjectionTree projectionTree;
86  
87  	/** The select box to choose the destination set. */
88  	private final JComboBox desitnationSetSelector = new JComboBox();
89  
90  	/**
91  	 * Instantiates a new move assignment to set dialog.
92  	 * 
93  	 * @param parameterAssignment
94  	 *          the parameter assignment
95  	 * @param projTree
96  	 *          the projection tree
97  	 * @param assignmentSet
98  	 *          the set the assignment is currently stored in
99  	 * @param currentSetType
100 	 *          the current Settype
101 	 * @param node
102 	 *          the node of the current set
103 	 */
104 	public MoveAssignmentToSetDialog(ParameterAssignment parameterAssignment,
105 	    IProjectionTree projTree, Set assignmentSet, SetType currentSetType) {
106 		assignment = parameterAssignment;
107 		oldSet = assignmentSet;
108 		projectionTree = projTree;
109 		setType = currentSetType;
110 		initUI();
111 	}
112 
113 	/*
114 	 * (non-Javadoc)
115 	 * 
116 	 * @see p3j.gui.dialogs.ProjectionDialog#addOKButtonAction()
117 	 */
118 	@Override
119 	protected void addOKButtonAction() {
120 		getOkButton().addActionListener(new OKActionListener());
121 	}
122 
123 	/**
124 	 * Initialize the user interface.
125 	 */
126 	private void initUI() {
127 		setTitle("Moving a parameter assignment");
128 		setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
129 		setModal(true);
130 		GUI.centerOnScreen(this);
131 
132 		List<Set> sets = new ArrayList<Set>(setType.getSets());
133 		sets.remove(oldSet);
134 
135 		for (Set s : sets) {
136 			desitnationSetSelector.addItem(s);
137 		}
138 
139 		PropertiesShowPanelFactory pspf = new PropertiesShowPanelFactory(
140 		    getButtons(), 1);
141 		pspf.sep("Assignment '" + assignment.getName() + "'");
142 		pspf.app("Move to set:", desitnationSetSelector);
143 		getContentPane().add(pspf.constructPanel());
144 	}
145 
146 	/**
147 	 * Checks if showing the dialog is meaningful.
148 	 * 
149 	 * @return true, if showing is meaningful because there is at least one other
150 	 *         set
151 	 */
152 	public boolean isMeaningful() {
153 		return desitnationSetSelector.getItemCount() > 0;
154 	}
155 }