001: package net.sourceforge.squirrel_sql.plugins.graph;
002:
003: import net.sourceforge.squirrel_sql.client.session.ISession;
004: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
005: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
006: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
007: import net.sourceforge.squirrel_sql.fw.util.StringManager;
008: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
009: import net.sourceforge.squirrel_sql.plugins.graph.xmlbeans.FormatXmlBean;
010:
011: import javax.swing.*;
012: import javax.swing.event.ListSelectionEvent;
013: import javax.swing.event.ListSelectionListener;
014: import java.awt.event.*;
015: import java.io.File;
016: import java.util.Arrays;
017: import java.util.Iterator;
018: import java.util.Vector;
019:
020: public class FormatController {
021: private static final StringManager s_stringMgr = StringManagerFactory
022: .getStringManager(FormatController.class);
023:
024: private FormatDlg _dlg;
025: private FormatXmlBean[] _formats;
026: private ISession _session;
027: private GraphPlugin _plugin;
028: private String FORMAT_XML_FILE_NAME = "formats.xml";
029:
030: private FormatControllerListener _listener;
031: private JPopupMenu m_lstPopup;
032: private Unit m_currentUnit;
033:
034: public FormatController(ISession session, GraphPlugin plugin,
035: FormatControllerListener listener) {
036: try {
037: _plugin = plugin;
038: _session = session;
039: _listener = listener;
040:
041: String userSettingsFolder = _plugin
042: .getPluginUserSettingsFolder().getPath();
043:
044: File f = new File(userSettingsFolder + File.separator
045: + FORMAT_XML_FILE_NAME);
046:
047: if (f.exists()) {
048: XMLBeanReader br = new XMLBeanReader();
049: br.load(userSettingsFolder + File.separator
050: + FORMAT_XML_FILE_NAME, this .getClass()
051: .getClassLoader());
052:
053: Vector<FormatXmlBean> buf = new Vector<FormatXmlBean>();
054:
055: for (Iterator<?> i = br.iterator(); i.hasNext();) {
056: buf.add((FormatXmlBean) i.next());
057: }
058:
059: _formats = buf.toArray(new FormatXmlBean[buf.size()]);
060: }
061:
062: } catch (Exception e) {
063: throw new RuntimeException(e);
064: }
065: }
066:
067: private void onSave() {
068:
069: if (null == _dlg.txtName.getText()
070: || "".equals(_dlg.txtName.getText().trim())) {
071: // i18n[graph.invalidName=Invalid name]
072: JOptionPane.showMessageDialog(_dlg, s_stringMgr
073: .getString("graph.invalidName"));
074: return;
075: }
076: String name = _dlg.txtName.getText().trim();
077:
078: Unit selUnit = (Unit) _dlg.cboUnit.getSelectedItem();
079:
080: double height;
081: double width;
082:
083: String buf;
084: buf = _dlg.txtHeight.getText();
085: try {
086: height = Double.parseDouble(buf) * selUnit.getInCm();
087: } catch (NumberFormatException e) {
088: // i18n[graph.invalidHeight=Invalid height]
089: JOptionPane.showMessageDialog(_dlg, s_stringMgr
090: .getString("graph.invalidHeight"));
091: return;
092: }
093:
094: buf = _dlg.txtWidth.getText();
095: try {
096: width = Double.parseDouble(buf) * selUnit.getInCm();
097: } catch (NumberFormatException e) {
098: // i18n[graph.invalidWidth=Invalid width]
099: JOptionPane.showMessageDialog(_dlg, s_stringMgr
100: .getString("graph.invalidWidth"));
101: return;
102: }
103:
104: boolean isLandscape = _dlg.chkIsLandscape.isSelected();
105:
106: FormatXmlBean selBean = (FormatXmlBean) _dlg.lstFormats
107: .getSelectedValue();
108:
109: if (null == selBean) {
110: selBean = new FormatXmlBean(name, width, height, false,
111: isLandscape);
112: Vector<FormatXmlBean> v = new Vector<FormatXmlBean>();
113: v.addAll(Arrays.asList(_formats));
114: v.add(selBean);
115: _formats = v.toArray(new FormatXmlBean[v.size()]);
116:
117: _dlg.lstFormats.setListData(_formats);
118: _dlg.lstFormats.setSelectedValue(selBean, true);
119: } else {
120: selBean.setName(name);
121: selBean.setWidth(width);
122: selBean.setHeight(height);
123: selBean.setLandscape(isLandscape);
124: _dlg.lstFormats.repaint();
125: }
126:
127: _listener.formatsChanged((FormatXmlBean) _dlg.lstFormats
128: .getSelectedValue());
129: saveFormats();
130: }
131:
132: private void saveFormats() {
133: try {
134: String userSettingsFolder = _plugin
135: .getPluginUserSettingsFolder().getPath();
136: XMLBeanWriter bw = new XMLBeanWriter();
137: for (int i = 0; i < _formats.length; i++) {
138: bw.addToRoot(_formats[i]);
139: }
140: bw.save(userSettingsFolder + File.separator
141: + FORMAT_XML_FILE_NAME);
142: } catch (Exception e) {
143: throw new RuntimeException(e);
144: }
145: }
146:
147: private void onNew() {
148: _dlg.lstFormats.clearSelection();
149: }
150:
151: private void onListSelectionChanged(ListSelectionEvent e) {
152: if (false == e.getValueIsAdjusting()) {
153: updateRightSideControls();
154: }
155: }
156:
157: private void updateRightSideControls() {
158: FormatXmlBean selBean = (FormatXmlBean) _dlg.lstFormats
159: .getSelectedValue();
160:
161: if (null == selBean) {
162: _dlg.txtName.setText(null);
163: _dlg.txtHeight.setText(null);
164: _dlg.txtWidth.setText(null);
165: } else {
166: _dlg.txtName.setText(selBean.getName());
167: _dlg.chkIsLandscape.setSelected(selBean.isLandscape());
168:
169: Unit unit = (Unit) _dlg.cboUnit.getSelectedItem();
170:
171: if (_dlg.cboUnit.getSelectedItem() == Unit.UNIT_CM) {
172: _dlg.txtHeight.setText("" + selBean.getHeight());
173: _dlg.txtWidth.setText("" + selBean.getWidth());
174: } else {
175: _dlg.txtHeight.setText("" + selBean.getHeight()
176: / unit.getInCm());
177: _dlg.txtWidth.setText("" + selBean.getWidth()
178: / unit.getInCm());
179: }
180: }
181: }
182:
183: private FormatXmlBean[] getDefaultFormats() {
184: return new FormatXmlBean[] {
185: // i18n[graph.dina3=Din A 3]
186: new FormatXmlBean(s_stringMgr.getString("graph.dina3"),
187: 29.7, 42.0, false, false),
188: // i18n[graph.dina4=Din A 4]
189: new FormatXmlBean(s_stringMgr.getString("graph.dina4"),
190: 21.0, 29.7, false, false),
191: // i18n[graph.dina5=Din A 5]
192: new FormatXmlBean(s_stringMgr.getString("graph.dina5"),
193: 14.8, 21.0, true, false) };
194: }
195:
196: public void setVisible(boolean b) {
197: if (null == _dlg) {
198: _dlg = new FormatDlg(_session.getApplication()
199: .getMainFrame());
200:
201: if (null == _formats) {
202: _formats = getDefaultFormats();
203: }
204:
205: _dlg.lstFormats.setListData(_formats);
206:
207: _dlg.cboUnit.addItem(Unit.UNIT_CM);
208: _dlg.cboUnit.addItem(Unit.UNIT_INCH);
209: m_currentUnit = Unit.UNIT_CM;
210: _dlg.cboUnit.setSelectedItem(Unit.UNIT_CM);
211:
212: _dlg.lstFormats
213: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
214:
215: _dlg.lstFormats
216: .addListSelectionListener(new ListSelectionListener() {
217: public void valueChanged(ListSelectionEvent e) {
218: onListSelectionChanged(e);
219: }
220: });
221:
222: _dlg.lstFormats.addMouseListener(new MouseAdapter() {
223: public void mousePressed(MouseEvent e) {
224: maybeShowListPopUp(e);
225: }
226:
227: public void mouseReleased(MouseEvent e) {
228: maybeShowListPopUp(e);
229: }
230: });
231:
232: m_lstPopup = new JPopupMenu();
233:
234: // i18n[graph.mnuDelete=delete]
235: JMenuItem mnuDeleteFomat = new JMenuItem(s_stringMgr
236: .getString("graph.mnuDelete"));
237: mnuDeleteFomat.addActionListener(new ActionListener() {
238: public void actionPerformed(ActionEvent e) {
239: onDeleteSeletedListItems();
240: }
241: });
242: m_lstPopup.add(mnuDeleteFomat);
243:
244: // i18n[graph.landscape=landscape]
245: JMenuItem mnuLandscape = new JMenuItem(s_stringMgr
246: .getString("graph.landscape"));
247: mnuLandscape.addActionListener(new ActionListener() {
248: public void actionPerformed(ActionEvent e) {
249: onLandscape();
250: }
251: });
252: m_lstPopup.add(mnuLandscape);
253:
254: _dlg.btnNew.addActionListener(new ActionListener() {
255: public void actionPerformed(ActionEvent e) {
256: onNew();
257: }
258: });
259:
260: _dlg.btnSave.addActionListener(new ActionListener() {
261: public void actionPerformed(ActionEvent e) {
262: onSave();
263: }
264: });
265:
266: _dlg.addWindowListener(new WindowAdapter() {
267: public void windowClosing(WindowEvent e) {
268: onWindowClosing();
269: }
270: });
271:
272: _dlg.cboUnit.addItemListener(new ItemListener() {
273: public void itemStateChanged(ItemEvent e) {
274: onUnitChanged(e);
275: }
276: });
277:
278: }
279:
280: GUIUtils.centerWithinParent(_dlg);
281:
282: _dlg.setVisible(b);
283: }
284:
285: private void onLandscape() {
286: FormatXmlBean selBean = (FormatXmlBean) _dlg.lstFormats
287: .getSelectedValue();
288:
289: if (null == selBean) {
290: return;
291: }
292:
293: FormatXmlBean lsBean = new FormatXmlBean(selBean.getName()
294: + " (LS)", selBean.getHeight(), selBean.getWidth(),
295: false, true);
296:
297: Vector<FormatXmlBean> v = new Vector<FormatXmlBean>();
298: v.addAll(Arrays.asList(_formats));
299: v.add(lsBean);
300: _formats = v.toArray(new FormatXmlBean[v.size()]);
301:
302: _dlg.lstFormats.setListData(_formats);
303: _dlg.lstFormats.setSelectedValue(lsBean, true);
304:
305: _listener.formatsChanged((FormatXmlBean) _dlg.lstFormats
306: .getSelectedValue());
307:
308: _dlg.lstFormats.repaint();
309:
310: }
311:
312: private void onDeleteSeletedListItems() {
313: Object[] selFormats = _dlg.lstFormats.getSelectedValues();
314:
315: Vector<FormatXmlBean> remainFormats = new Vector<FormatXmlBean>();
316: for (int i = 0; i < _formats.length; i++) {
317: boolean found = false;
318: for (int j = 0; j < selFormats.length; j++) {
319: if (_formats[i] == selFormats[j]) {
320: found = true;
321: break;
322: }
323: }
324: if (false == found) {
325: remainFormats.add(_formats[i]);
326: }
327: }
328:
329: if (0 == remainFormats.size()) {
330: _formats = getDefaultFormats();
331: } else {
332: _formats = remainFormats
333: .toArray(new FormatXmlBean[remainFormats.size()]);
334: }
335:
336: saveFormats();
337:
338: _dlg.lstFormats.setListData(_formats);
339:
340: }
341:
342: private void maybeShowListPopUp(MouseEvent e) {
343: if (e.isPopupTrigger()) {
344: m_lstPopup.show(e.getComponent(), e.getX(), e.getY());
345: }
346: }
347:
348: private void onUnitChanged(ItemEvent e) {
349: if (ItemEvent.SELECTED == e.getStateChange()) {
350: double widht;
351: double height;
352:
353: Unit selUnit = (Unit) _dlg.cboUnit.getSelectedItem();
354:
355: if (selUnit == m_currentUnit) {
356: return;
357: }
358:
359: try {
360: widht = Double.parseDouble(_dlg.txtWidth.getText());
361: height = Double.parseDouble(_dlg.txtHeight.getText());
362: } catch (NumberFormatException e1) {
363: // i18n[graph.invalidNumberFormat=Invalid number format. Can not calculate new unit.]
364: JOptionPane.showMessageDialog(_dlg, s_stringMgr
365: .getString("graph.invalidNumberFormat"));
366:
367: SwingUtilities.invokeLater(new Runnable() {
368: public void run() {
369: _dlg.cboUnit.setSelectedItem(m_currentUnit);
370: }
371: });
372:
373: return;
374: }
375:
376: widht *= m_currentUnit.getInCm() / selUnit.getInCm();
377: height *= m_currentUnit.getInCm() / selUnit.getInCm();
378: _dlg.txtWidth.setText("" + widht);
379: _dlg.txtHeight.setText("" + height);
380:
381: m_currentUnit = selUnit;
382: }
383: }
384:
385: private void onWindowClosing() {
386: _listener.formatsChanged((FormatXmlBean) _dlg.lstFormats
387: .getSelectedValue());
388: saveFormats();
389: }
390:
391: public FormatXmlBean[] getFormats() {
392: if (null == _formats) {
393: _formats = getDefaultFormats();
394: }
395:
396: return _formats;
397: }
398:
399: public void close() {
400: if (null != _dlg) {
401: saveFormats();
402: _dlg.setVisible(false);
403: _dlg.dispose();
404: }
405:
406: }
407:
408: public static class Unit {
409: // i18n[graph.cm=cm]
410: public static final Unit UNIT_CM = new Unit(s_stringMgr
411: .getString("graph.cm"), 1);
412: // i18n[graph.inch=inch]
413: public static final Unit UNIT_INCH = new Unit(s_stringMgr
414: .getString("graph.inch"), 2.54);
415:
416: private String _name;
417: private double _inCm;
418:
419: private Unit(String name, double inCm) {
420: _name = name;
421: _inCm = inCm;
422: }
423:
424: public String getName() {
425: return _name;
426: }
427:
428: public double getInCm() {
429: return _inCm;
430: }
431:
432: public String toString() {
433: return _name;
434: }
435: }
436:
437: }
|