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  
21  import javax.swing.JButton;
22  import javax.swing.JDialog;
23  import javax.swing.JPanel;
24  
25  import p3j.gui.panels.projections.IProjectionTree;
26  import p3j.gui.panels.projections.ProjectionTreeNode;
27  import p3j.misc.gui.GUI;
28  
29  import com.jgoodies.forms.builder.ButtonBarBuilder2;
30  
31  /**
32   * 
33   * Super class for all dialogs that need to modify an {@link IProjectionTree}.
34   * 
35   * Created: September 7, 2008
36   * 
37   * @param <E>
38   *          type of the entity
39   * @param <N>
40   *          type of the node representing the entity
41   * 
42   * @author Christina Bohk
43   * @author Roland Ewald
44   * 
45   */
46  public abstract class AbstractProjectionTreeDialog<E, N extends ProjectionTreeNode<E>>
47      extends JDialog {
48  
49  	/** Serialization ID. */
50  	private static final long serialVersionUID = -1735341315107050803L;
51  
52  	/** The entity to be edited. */
53  	private final E entity;
54  
55  	/** The node belonging to the edited entity. */
56  	private final N entityNode;
57  
58  	/** The projection tree (needs to be notified). */
59  	private IProjectionTree projectionTree;
60  
61  	/** Cancel Settype creation. */
62  	private final JButton cancelButton = new JButton("Cancel");
63  	{
64  		cancelButton.addActionListener(new ActionListener() {
65  			@Override
66  			public void actionPerformed(ActionEvent e) {
67  				cancel();
68  			}
69  		});
70  	}
71  
72  	/** Confirm Settype creation. */
73  	private final JButton okButton = new JButton("OK");
74  	{
75  		okButton.addActionListener(new ActionListener() {
76  			@Override
77  			public void actionPerformed(ActionEvent e) {
78  				ok();
79  			}
80  		});
81  	}
82  
83  	/**
84  	 * Default constructor.
85  	 * 
86  	 * @param projNode
87  	 *          the node representing the {@link p3j.pppm.ProjectionModel} which
88  	 *          shall be modified
89  	 * @param projTree
90  	 *          the projection tree
91  	 * @param width
92  	 *          the width of the dialog
93  	 * @param height
94  	 *          the height of the dialog
95  	 * @param okButtonText
96  	 *          the text to be displayed on the OK button
97  	 */
98  	public AbstractProjectionTreeDialog(N projNode, IProjectionTree projTree,
99  	    int width, int height, String okButtonText) {
100 		entity = projNode.getEntity();
101 		entityNode = projNode;
102 		setProjectionTree(projTree);
103 		okButton.setText(okButtonText);
104 		setSize(width, height);
105 		GUI.centerOnScreen(this);
106 	}
107 
108 	/**
109 	 * Creates a panel containing the cancel and the OK button.
110 	 * 
111 	 * @return a panel containing the cancel and the OK button
112 	 */
113 	protected JPanel getButtonPanel() {
114 		ButtonBarBuilder2 bbBuilder = new ButtonBarBuilder2();
115 		bbBuilder.addButton(okButton);
116 		bbBuilder.addRelatedGap();
117 		bbBuilder.addButton(cancelButton);
118 		return bbBuilder.getPanel();
119 	}
120 
121 	/**
122 	 * Will be called when user pressed OK button.
123 	 */
124 	protected abstract void ok();
125 
126 	/**
127 	 * Will be called when user cancels dialog.
128 	 */
129 	protected abstract void cancel();
130 
131 	public N getEntityNode() {
132 		return entityNode;
133 	}
134 
135 	public IProjectionTree getProjectionTree() {
136 		return projectionTree;
137 	}
138 
139 	public final void setProjectionTree(IProjectionTree projectionTree) {
140 		this.projectionTree = projectionTree;
141 	}
142 
143 	public E getEntity() {
144 		return entity;
145 	}
146 }