1 30 package com.genimen.djeneric.tools.specifier.dialogs; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Frame ; 35 import java.awt.Toolkit ; 36 import java.awt.event.ActionEvent ; 37 import java.util.ArrayList ; 38 import java.util.Properties ; 39 40 import javax.swing.JButton ; 41 import javax.swing.JDialog ; 42 import javax.swing.JOptionPane ; 43 import javax.swing.JPanel ; 44 45 import com.genimen.djeneric.language.Messages; 46 import com.genimen.djeneric.repository.DjExtent; 47 import com.genimen.djeneric.repository.DjObject; 48 import com.genimen.djeneric.repository.DjSession; 49 import com.genimen.djeneric.tools.common.DjenericTool; 50 import com.genimen.djeneric.tools.specifier.editor.ChooserPanel; 51 import com.genimen.djeneric.ui.Util; 52 import com.genimen.djeneric.util.DjLogger; 53 54 public class ChooserDialog extends JDialog 55 { 56 private static final long serialVersionUID = 1L; 57 JPanel panel1 = new JPanel (); 58 BorderLayout borderLayout1 = new BorderLayout (); 59 ChooserPanel _chooserPanel; 60 JPanel _buttonPanel = new JPanel (); 61 BorderLayout borderLayout2 = new BorderLayout (); 62 JPanel jPanel3 = new JPanel (); 63 JButton _butOk = new JButton (); 64 JButton _butCancel = new JButton (); 65 boolean _wasCancelled = true; 66 DjObject _selectedObject = null; 67 boolean _required; 68 boolean _valueWasSelected = false; 69 JButton _butNone = new JButton (); 70 71 public ChooserDialog(Frame frame, DjSession session, DjExtent extent, ArrayList excludeTheseProperties, String title, 72 boolean required, int x, int y) 73 { 74 super(frame, (title == null ? Messages.getString("ChooserDialog.Choose", extent.getNameSingular()) : title), true); 75 try 76 { 77 _required = required; 78 _chooserPanel = new ChooserPanel(session, extent) 79 { 80 private static final long serialVersionUID = 1L; 81 82 public void acceptHit() 83 { 84 _butOk_actionPerformed(null); 85 } 86 87 public void escapeHit() 88 { 89 _butCancel_actionPerformed(null); 90 } 91 }; 92 93 _chooserPanel.hideDefaultOkCCancelButtons(true); 94 95 jbInit(); 96 _butNone.setVisible(!required); 97 98 if (_chooserPanel.getQueryableProperties().length > 5) 99 { 100 int w = _chooserPanel.getPreferredSize().width * 2 + 20; 101 int h = _chooserPanel.getPreferredSize().height / 2 + 40; 102 _chooserPanel.setPreferredSize(new Dimension (w, h)); 103 } 104 105 int calculatedWidth = Math.max(_chooserPanel.getPreferredSize().width + 10, panel1.getPreferredSize().width + 10); 106 int calculatedHeight = _chooserPanel.getPreferredSize().height + _buttonPanel.getPreferredSize().height + 50; 107 108 Dimension dim = getStoredDimension(calculatedWidth, calculatedHeight); 109 110 setSize(dim); 111 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 112 Dimension frameSize = getSize(); 113 114 if (x != 0) 115 { 116 if (x + frameSize.width > screenSize.width) x = screenSize.width - frameSize.width; 117 if (y + frameSize.height > screenSize.height) y = screenSize.height - frameSize.height; 118 if (x < 0) x = 0; 119 if (y < 0) y = 0; 120 setLocation(x, y); 121 } 122 else 123 { 124 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 125 } 126 setVisible(true); 127 } 128 catch (Exception ex) 129 { 130 DjLogger.log(ex); 131 } 132 } 133 134 private Dimension getStoredDimension(int calculatedWidth, int calculatedHeight) 135 { 136 int width = calculatedWidth; 137 int height = calculatedHeight; 138 139 Properties props = getProperties(); 140 if (props != null) 141 { 142 String chooserId = getChooserId(); 143 width = Integer.parseInt(props.getProperty(chooserId + ".width", "" + calculatedWidth)); 144 height = Integer.parseInt(props.getProperty(chooserId + ".height", "" + calculatedWidth)); 145 if (width < calculatedWidth) width = calculatedWidth; 146 if (height < calculatedHeight) height = calculatedHeight; 147 } 148 149 return new Dimension (width, height); 150 } 151 152 private Properties getProperties() 153 { 154 Frame frame = Util.findActiveFrame(); 155 if (frame instanceof DjenericTool) 156 { 157 DjenericTool tool = (DjenericTool) frame; 158 return tool.getProperties(); 159 } 160 return null; 161 } 162 163 private String getChooserId() 164 { 165 String title = getTitle(); 166 StringBuffer id = new StringBuffer (50); 167 for (int i = 0; i < title.length(); i++) 168 if (Character.isJavaIdentifierPart(title.charAt(i))) id.append(title.charAt(i)); 169 if (id.length() == 0) id.append("genericchooser"); 170 return id.toString(); 171 } 172 173 void jbInit() throws Exception 174 { 175 panel1.setLayout(borderLayout1); 176 _buttonPanel.setLayout(borderLayout2); 177 _butOk.setText(Messages.getString("global.Ok")); 178 _butOk.addActionListener(new java.awt.event.ActionListener () 179 { 180 public void actionPerformed(ActionEvent e) 181 { 182 _butOk_actionPerformed(e); 183 } 184 }); 185 _butCancel.setText(Messages.getString("global.Cancel")); 186 _butCancel.addActionListener(new java.awt.event.ActionListener () 187 { 188 public void actionPerformed(ActionEvent e) 189 { 190 _butCancel_actionPerformed(e); 191 } 192 }); 193 _butNone.setOpaque(true); 194 _butNone.setText(Messages.getString("global.None")); 195 _butNone.addActionListener(new java.awt.event.ActionListener () 196 { 197 public void actionPerformed(ActionEvent e) 198 { 199 _butNone_actionPerformed(e); 200 } 201 }); 202 getContentPane().add(panel1); 203 204 jPanel3.add(_butCancel, null); 205 jPanel3.add(_butNone, null); 206 jPanel3.add(_butOk, null); 207 _buttonPanel.add(jPanel3, BorderLayout.EAST); 208 209 panel1.add(_buttonPanel, BorderLayout.SOUTH); 210 panel1.add(_chooserPanel, BorderLayout.CENTER); 211 Util.sizeButtons(jPanel3); 212 } 213 214 public boolean wasCancelled() 215 { 216 return _wasCancelled; 217 } 218 219 void _butOk_actionPerformed(ActionEvent e) 220 { 221 try 222 { 223 _selectedObject = _chooserPanel.getSelectedObject(); 224 if (_selectedObject != null) 225 { 226 _wasCancelled = false; 227 _valueWasSelected = true; 228 229 storeDimensions(); 230 setVisible(false); 231 } 232 } 233 catch (Exception x) 234 { 235 JOptionPane.showMessageDialog(this, x.getMessage(), Messages.getString("global.InvalidEntry"), 236 JOptionPane.WARNING_MESSAGE); 237 } 238 } 239 240 private void storeDimensions() 241 { 242 Properties props = getProperties(); 243 if (props != null) 244 { 245 String chooserId = getChooserId(); 246 props.setProperty(chooserId + ".width", "" + getWidth()); 247 props.setProperty(chooserId + ".height", "" + getHeight()); 248 } 249 } 250 251 void _butCancel_actionPerformed(ActionEvent e) 252 { 253 _valueWasSelected = false; 254 storeDimensions(); 255 setVisible(false); 256 257 } 258 259 public DjObject getSelectedObject() 260 { 261 if (_valueWasSelected) return _selectedObject; 262 else return null; 263 } 264 265 void _butNone_actionPerformed(ActionEvent e) 266 { 267 _valueWasSelected = false; 268 _selectedObject = null; 269 _wasCancelled = false; 270 setVisible(false); 271 } 272 273 } | Popular Tags |