1 package devisor2.grid.GUI.dialogs;
2 
3 import java.util.*;
4 
5 import java.awt.*;
6 import java.awt.event.*;
7 import javax.swing.*;
8 import javax.swing.border.*;
9 
10 import devisor2.grid.options.*;
11 import devisor2.grid.GUI.framework.*;
12 import devisor2.foundation.elements.*;
13 import devisor2.foundation.base.*;
14 import devisor2.foundation.boundary.*;
15 
16 
17 /**
18  *  See the GridListener for a detailed description of how the dialog
19  *  is connected to the application.
20  *
21  *  @author Dominik Goeddeke
22  *  @version yes
23  *
24  */
25 
26 public class MergeBoundariesDialog extends DockableDialog implements ActionListener
27 {
28 
29     /**
30      *  returns the initial Position of the dialog
31      */
getInitialPosition()32     public Point getInitialPosition ()
33     {
34 	return new Point (parent.getLocationOnScreen().x - 50, parent.getLocationOnScreen().y);
35     }
36 
37 
38     /**
39      *  the constructor just sets references correctly and creates the GUI
40      */
MergeBoundariesDialog(MainFrame parent)41     public MergeBoundariesDialog (MainFrame parent)
42     {
43 	super (parent);
44 	setTitle (cc.wS("dialogs_mergeboundaries_caption"));
45 	JPanel content = new JPanel ();
46 	content.setBorder (new EmptyBorder (10,10,10,10));
47 	// just ordinary GUI layouting
48 	GridBagLayout layout = new GridBagLayout ();
49 	GridBagConstraints constraints = new GridBagConstraints ();
50 	content.setLayout (layout);
51 	constraints.fill = GridBagConstraints.BOTH;
52 	constraints.weightx = constraints.weighty = 1.0;
53 	// combo1
54 	JLabel label = new JLabel ((String)cc.rb.getObject ("dialogs_mergeboundaries_targetlabel"));
55 	constraints.gridy = 0;
56 	constraints.gridx = 0;
57 	layout.setConstraints (label, constraints);
58 	content.add (label);
59 	targetCombo = new JComboBox ();
60 	targetCombo.setToolTipText ((String)cc.rb.getObject("dialogs_mergeboundaries_targettooltip"));
61 	constraints.gridx = 1;
62 	layout.setConstraints (targetCombo, constraints);
63 	targetCombo.setEditable (false);
64 	content.add (targetCombo);
65 	// combo1
66 	label = new JLabel ((String)cc.rb.getObject ("dialogs_mergeboundaries_sourcelabel"));
67 	constraints.gridy = 1;
68 	constraints.gridx = 0;
69 	layout.setConstraints (label, constraints);
70 	content.add (label);
71 	sourceCombo = new JComboBox ();
72 	sourceCombo.setToolTipText ((String)cc.rb.getObject("dialogs_mergeboundaries_sourcetooltip"));
73 	constraints.gridx = 1;
74 	layout.setConstraints (sourceCombo, constraints);
75 	sourceCombo.setEditable (false);
76 	content.add (sourceCombo);
77 	// labelling
78 	//	updateCombo ();
79 	// buttons
80 	JPanel buttons = new JPanel ();
81 	buttons.setLayout (new FlowLayout ());
82 	okButton = new JButton ((String)cc.rb.getObject ("general_Ok"));
83 	okButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Ok_mnemonic")).intValue());
84 	okButton.setActionCommand (OK);
85 	okButton.addActionListener (this);
86 	buttons.add (okButton);
87 	applyButton = new JButton ((String)cc.rb.getObject ("general_Apply"));
88 	applyButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Apply_mnemonic")).intValue());
89 	applyButton.setActionCommand (APPLY);
90 	applyButton.addActionListener (this);
91 	buttons.add (applyButton);
92 	cancelButton = new JButton ((String)cc.rb.getObject ("general_Cancel"));
93 	cancelButton.setMnemonic ( ((Integer)cc.rb.getObject("general_Cancel_mnemonic")).intValue());
94 	cancelButton.setActionCommand (CANCEL);
95 	cancelButton.addActionListener (this);
96 	buttons.add (cancelButton);
97 	// add them all to the dialog
98 	getRootPane().setDefaultButton (applyButton);
99 	constraints.gridy = 3;
100 	constraints.gridx = 0;
101 	constraints.gridwidth = 2;
102 	layout.setConstraints(buttons, constraints);
103 	content.add (buttons);
104 	// finish the GUI
105 	getContentPane ().add (content);
106 	addComponentListener (listener);
107 	pack ();
108     }
109 
110     /**
111      *  updates the boundary comboboxes
112      */
refreshGUI()113     public void refreshGUI ()
114     {
115 	targetCombo.removeActionListener (this);
116 	sourceCombo.removeActionListener (this);
117 	targetCombo.removeAllItems ();
118 	sourceCombo.removeAllItems ();
119 	if (cc.d.db.getBoundaryCount () == 0)
120 	{
121 	    targetCombo.addItem ("-1");
122 	    targetCombo.setSelectedItem ("-1");
123 	    sourceCombo.addItem ("-1");
124 	    sourceCombo.setSelectedItem ("-1");
125 	}
126 	else
127 	{
128 	    for (int i=0;i<cc.d.db.getBoundaryCount();i++)
129 	    {
130 		targetCombo.addItem (Integer.toString (i+1));
131 		sourceCombo.addItem (Integer.toString (i+1));
132 	    }
133 	    targetCombo.setSelectedItem (Integer.toString(cc.gc.workingBoundary.getNumber ()+1));
134 	    sourceCombo.setSelectedItem (Integer.toString(cc.gc.workingBoundary.getNumber ()+1));
135 	}
136 	targetCombo.addActionListener (this);
137 	sourceCombo.addActionListener (this);
138 
139     }
140 
141 
142     /**
143      *  This class handles its own action event.
144      */
actionPerformed(ActionEvent e)145     public void actionPerformed (ActionEvent e)
146     {
147 	if (e.getActionCommand().equals (OK))
148 	{
149 	    if (verify ())
150 	    {
151 		performMerge ();
152 		cc.dm.hide (cc.dm.MERGEBOUNDARIESDIALOG);
153 	    }
154 	    return;
155 	}
156 	if (e.getActionCommand().equals (APPLY))
157 	{
158 	    if (verify ())
159 	    {
160 		performMerge ();
161 	    }
162 	    return;
163 	}
164 	if (e.getActionCommand().equals (CANCEL))
165 	{
166 	    cc.dm.hide (cc.dm.MERGEBOUNDARIESDIALOG);
167 	    return;
168 	}
169     }
170 
171     /**
172      *  the verifivation code is all put into this method
173      */
verify()174     private boolean verify ()
175     {
176 	target = cc.d.db.getBoundary(Integer.parseInt ((String)targetCombo.getSelectedItem())-1);
177 	source = cc.d.db.getBoundary(Integer.parseInt ((String)sourceCombo.getSelectedItem())-1);
178 	if (target==source)
179 	{
180 	    JOptionPane.showMessageDialog (parent, (String)cc.rb.getObject("dialogs_mergeboundaries_error_sameboundaries"), (String)cc.rb.getObject ("errors_parseerror"), JOptionPane.ERROR_MESSAGE);
181 	    return false;
182 	}
183 	return true;
184     }
185 
186     /**
187      *  the actual merging is implemented in this method
188      */
performMerge()189     private void performMerge ()
190     {
191 	// segments are updated
192 	Enumeration segs = source.getSegments().elements();
193 	while (segs.hasMoreElements())
194 	{
195 	    Segment s = (Segment)segs.nextElement ();
196 	    s.setBoundary (target);
197 	}
198 	// and of course also bns
199 	Enumeration nodes = cc.d.getNodes().elements();
200 	while (nodes.hasMoreElements())
201 	{
202 	    Node n = (Node)nodes.nextElement ();
203 	    if (n instanceof BoundaryNode)
204 	    {
205 		BoundaryNode bn = (BoundaryNode)n;
206 		if (bn.getBoundary()==source)
207 		    bn.setBoundary (target);
208 	    }
209 	}
210 	// and after all. try to fix parametrisation
211 	int tmp = cc.dc.tolerance;
212 	cc.dc.tolerance=1;
213 	cc.d.db.continousBoundary();
214 	cc.dc.tolerance = tmp;
215     }
216 
217     private static final String OK = "OK";
218     private static final String CANCEL = "CANCEL";
219     private static final String APPLY = "APPLY";
220 
221     private JButton okButton;
222     private JButton cancelButton;
223     private JButton applyButton;
224 
225     private JComboBox targetCombo;
226     private JComboBox sourceCombo;
227 
228     private Boundary target;
229     private Boundary source;
230 
231 
232 }
233