001: package net.sf.memoranda.ui;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Dimension;
006: import java.awt.FlowLayout;
007: import java.awt.Frame;
008: import java.awt.GridBagConstraints;
009: import java.awt.GridBagLayout;
010: import java.awt.Insets;
011: import java.awt.event.ActionEvent;
012:
013: import javax.swing.BorderFactory;
014: import javax.swing.ButtonGroup;
015: import javax.swing.ImageIcon;
016: import javax.swing.JButton;
017: import javax.swing.JDialog;
018: import javax.swing.JFileChooser;
019: import javax.swing.JLabel;
020: import javax.swing.JPanel;
021: import javax.swing.JRadioButton;
022: import javax.swing.JCheckBox;
023: import javax.swing.JTextField;
024: import javax.swing.UIManager;
025: import javax.swing.event.CaretEvent;
026:
027: import net.sf.memoranda.util.Local;
028:
029: /*$Id: AddResourceDialog.java,v 1.12 2007/03/20 06:21:46 alexeya Exp $*/
030: public class AddResourceDialog extends JDialog {
031: JPanel dialogTitlePanel = new JPanel(
032: new FlowLayout(FlowLayout.LEFT));
033: JLabel header = new JLabel();
034: ButtonGroup buttonGroup1 = new ButtonGroup();
035: JPanel areaPanel = new JPanel(new GridBagLayout());
036: GridBagConstraints gbc;
037: public JRadioButton localFileRB = new JRadioButton();
038: public JCheckBox projectFileCB = new JCheckBox(
039: "Copy file to memoranda", false);
040: JLabel jLabel1 = new JLabel();
041: public JTextField pathField = new JTextField();
042: JButton browseB = new JButton();
043: JLabel jLabel2 = new JLabel();
044: public JRadioButton inetShortcutRB = new JRadioButton();
045: public JTextField urlField = new JTextField();
046: JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,
047: 15, 15));
048: JButton okB = new JButton();
049: JButton cancelB = new JButton();
050: public boolean CANCELLED = true;
051:
052: public AddResourceDialog(Frame frame, String title) {
053: super (frame, title, true);
054: try {
055: jbInit();
056: pack();
057: } catch (Exception ex) {
058: new ExceptionDialog(ex);
059: ex.printStackTrace();
060: }
061: }
062:
063: /**
064: * setup user interface and init dialog
065: */
066:
067: void jbInit() throws Exception {
068: this .setResizable(false);
069: dialogTitlePanel.setBackground(Color.WHITE);
070: dialogTitlePanel.setBorder(BorderFactory.createEmptyBorder(0,
071: 5, 0, 5));
072: header.setFont(new java.awt.Font("Dialog", 0, 20));
073: header.setForeground(new Color(0, 0, 124));
074: header.setText(Local.getString("New resource"));
075: header
076: .setIcon(new ImageIcon(
077: net.sf.memoranda.ui.AddResourceDialog.class
078: .getResource("resources/icons/resource48.png")));
079: dialogTitlePanel.add(header);
080: this .getContentPane().add(dialogTitlePanel, BorderLayout.NORTH);
081:
082: buttonGroup1.add(localFileRB);
083: buttonGroup1.add(inetShortcutRB);
084: localFileRB.setSelected(true);
085: localFileRB.setText(Local.getString("Local file"));
086: localFileRB
087: .addActionListener(new java.awt.event.ActionListener() {
088: public void actionPerformed(ActionEvent e) {
089: localFileRB_actionPerformed(e);
090: }
091: });
092: gbc = new GridBagConstraints();
093: gbc.gridwidth = 2;
094: gbc.gridx = 0;
095: gbc.gridy = 0;
096: gbc.insets = new Insets(10, 15, 5, 15);
097: gbc.anchor = GridBagConstraints.WEST;
098: gbc.fill = GridBagConstraints.HORIZONTAL;
099: areaPanel.add(localFileRB, gbc);
100:
101: gbc = new GridBagConstraints();
102: gbc.gridwidth = 2;
103: gbc.gridx = 2;
104: gbc.gridy = 0;
105: gbc.insets = new Insets(10, 15, 5, 15);
106: gbc.anchor = GridBagConstraints.WEST;
107: gbc.fill = GridBagConstraints.HORIZONTAL;
108: areaPanel.add(projectFileCB, gbc);
109:
110: jLabel1.setText(Local.getString("Path") + ": ");
111: gbc = new GridBagConstraints();
112: gbc.gridx = 0;
113: gbc.gridy = 1;
114: gbc.insets = new Insets(5, 20, 5, 5);
115: gbc.anchor = GridBagConstraints.WEST;
116: gbc.fill = GridBagConstraints.HORIZONTAL;
117: areaPanel.add(jLabel1, gbc);
118: pathField.setMinimumSize(new Dimension(4, 24));
119: pathField.setPreferredSize(new Dimension(250, 24));
120: pathField
121: .addCaretListener(new javax.swing.event.CaretListener() {
122: public void caretUpdate(CaretEvent e) {
123: pathField_caretUpdate(e);
124: }
125: });
126: gbc = new GridBagConstraints();
127: gbc.gridx = 1;
128: gbc.gridy = 1;
129: gbc.insets = new Insets(5, 5, 5, 5);
130: gbc.anchor = GridBagConstraints.WEST;
131: gbc.fill = GridBagConstraints.HORIZONTAL;
132: areaPanel.add(pathField, gbc);
133: browseB.setText(Local.getString("Browse"));
134: browseB.addActionListener(new java.awt.event.ActionListener() {
135: public void actionPerformed(ActionEvent e) {
136: browseB_actionPerformed(e);
137: }
138: });
139: gbc = new GridBagConstraints();
140: gbc.gridx = 2;
141: gbc.gridy = 1;
142: gbc.insets = new Insets(5, 10, 5, 15);
143: gbc.anchor = GridBagConstraints.WEST;
144: areaPanel.add(browseB, gbc);
145: inetShortcutRB.setText(Local.getString("Internet shortcut"));
146: inetShortcutRB
147: .addActionListener(new java.awt.event.ActionListener() {
148: public void actionPerformed(ActionEvent e) {
149: inetShortcutRB_actionPerformed(e);
150: }
151: });
152: gbc = new GridBagConstraints();
153: gbc.gridx = 0;
154: gbc.gridy = 2;
155: gbc.gridwidth = 2;
156: gbc.insets = new Insets(5, 15, 5, 15);
157: gbc.anchor = GridBagConstraints.WEST;
158: areaPanel.add(inetShortcutRB, gbc);
159: jLabel2.setText(Local.getString("URL") + ": ");
160: gbc = new GridBagConstraints();
161: gbc.gridx = 0;
162: gbc.gridy = 3;
163: gbc.insets = new Insets(5, 20, 5, 15);
164: gbc.anchor = GridBagConstraints.WEST;
165: areaPanel.add(jLabel2, gbc);
166: urlField.setMinimumSize(new Dimension(4, 24));
167: urlField.setPreferredSize(new Dimension(335, 24));
168: urlField
169: .addCaretListener(new javax.swing.event.CaretListener() {
170: public void caretUpdate(CaretEvent e) {
171: urlField_caretUpdate(e);
172: }
173: });
174: gbc = new GridBagConstraints();
175: gbc.gridx = 1;
176: gbc.gridy = 3;
177: gbc.gridwidth = 2;
178: gbc.insets = new Insets(5, 5, 0, 15);
179: gbc.anchor = GridBagConstraints.WEST;
180: gbc.fill = GridBagConstraints.HORIZONTAL;
181: areaPanel.add(urlField, gbc);
182: this .getContentPane().add(areaPanel, BorderLayout.CENTER);
183:
184: okB.setEnabled(false);
185: okB.setMaximumSize(new Dimension(100, 26));
186: okB.setMinimumSize(new Dimension(100, 26));
187: okB.setPreferredSize(new Dimension(100, 26));
188: okB.setText(Local.getString("Ok"));
189: okB.addActionListener(new java.awt.event.ActionListener() {
190: public void actionPerformed(ActionEvent e) {
191: okB_actionPerformed(e);
192: }
193: });
194: this .getRootPane().setDefaultButton(okB);
195: cancelB.setMaximumSize(new Dimension(100, 26));
196: cancelB.setMinimumSize(new Dimension(100, 26));
197: cancelB.setPreferredSize(new Dimension(100, 26));
198: cancelB.setText(Local.getString("Cancel"));
199: cancelB.addActionListener(new java.awt.event.ActionListener() {
200: public void actionPerformed(ActionEvent e) {
201: cancelB_actionPerformed(e);
202: }
203: });
204: buttonsPanel.add(okB);
205: buttonsPanel.add(cancelB);
206: enableFields();
207: this .getContentPane().add(buttonsPanel, BorderLayout.SOUTH);
208: }
209:
210: /**
211: * set CANCELLED variable to false so we can know the user
212: * pressed the ok buton and close this dialog.
213: */
214:
215: void okB_actionPerformed(ActionEvent e) {
216: CANCELLED = false;
217: this .dispose();
218: }
219:
220: /**
221: * close the dialog window
222: */
223:
224: void cancelB_actionPerformed(ActionEvent e) {
225: this .dispose();
226: }
227:
228: /**
229: * enable localRB fields. Request focus for the text field
230: * so the user can start typing and set the pathField text selected
231: */
232:
233: void localFileRB_actionPerformed(ActionEvent e) {
234: enableFields();
235: checkOkEnabled();
236: urlField.select(0, 0);
237: pathField.select(0, pathField.getText().length());
238: pathField.requestFocus();
239: }
240:
241: /**
242: * enable inetShorcutRB fields. Request focus for the text field
243: * so the user can start typing and set the urlField text selected
244: */
245:
246: void inetShortcutRB_actionPerformed(ActionEvent e) {
247: enableFields();
248: checkOkEnabled();
249: pathField.select(0, 0);
250: urlField.select(0, urlField.getText().length());
251: urlField.requestFocus();
252: }
253:
254: /**
255: * setup the JFileChooser so the user can select the resource file
256: */
257:
258: void browseB_actionPerformed(ActionEvent e) {
259: // Fix until Sun's JVM supports more locales...
260: UIManager.put("FileChooser.lookInLabelText", Local
261: .getString("Look in:"));
262: UIManager.put("FileChooser.upFolderToolTipText", Local
263: .getString("Up One Level"));
264: UIManager.put("FileChooser.newFolderToolTipText", Local
265: .getString("Create New Folder"));
266: UIManager.put("FileChooser.listViewButtonToolTipText", Local
267: .getString("List"));
268: UIManager.put("FileChooser.detailsViewButtonToolTipText", Local
269: .getString("Details"));
270: UIManager.put("FileChooser.fileNameLabelText", Local
271: .getString("File Name:"));
272: UIManager.put("FileChooser.filesOfTypeLabelText", Local
273: .getString("Files of Type:"));
274: UIManager.put("FileChooser.openButtonText", Local
275: .getString("Open"));
276: UIManager.put("FileChooser.openButtonToolTipText", Local
277: .getString("Open selected file"));
278: UIManager.put("FileChooser.cancelButtonText", Local
279: .getString("Cancel"));
280: UIManager.put("FileChooser.cancelButtonToolTipText", Local
281: .getString("Cancel"));
282:
283: JFileChooser chooser = new JFileChooser();
284: chooser.setFileHidingEnabled(false);
285: chooser.setDialogTitle(Local.getString("Add resource"));
286: chooser.setAcceptAllFileFilterUsed(true);
287: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
288: chooser.setPreferredSize(new Dimension(550, 375));
289: /*java.io.File lastSel = (java.io.File) Context.get("LAST_SELECTED_RESOURCE_FILE");
290: if (lastSel != null)
291: chooser.setCurrentDirectory(lastSel);*/
292: if (chooser.showOpenDialog(this ) != JFileChooser.APPROVE_OPTION)
293: return;
294: /*try {
295: Context.put("LAST_SELECTED_RESOURCE_FILE", chooser.getSelectedFile());
296: }
297: catch (Exception ex) {}*/
298: pathField.setText(chooser.getSelectedFile().getPath());
299: checkOkEnabled();
300: }
301:
302: /**
303: * disable the ok button if pathField is empty
304: */
305:
306: void pathField_caretUpdate(CaretEvent e) {
307: checkOkEnabled();
308: }
309:
310: /**
311: * disable the ok button if urlField is empty
312: */
313:
314: void urlField_caretUpdate(CaretEvent e) {
315: checkOkEnabled();
316: }
317:
318: /**
319: * do not enable the ok button until the text field is not empty.
320: */
321:
322: void checkOkEnabled() {
323: okB.setEnabled((localFileRB.isSelected() && pathField.getText()
324: .length() > 0)
325: || (inetShortcutRB.isSelected() && urlField.getText()
326: .length() > 0));
327: }
328:
329: /**
330: * enable and disable fields when user selects the radio buttons options
331: */
332:
333: void enableFields() {
334: pathField.setEnabled(localFileRB.isSelected());
335: jLabel1.setEnabled(localFileRB.isSelected());
336: browseB.setEnabled(localFileRB.isSelected());
337: projectFileCB.setEnabled(localFileRB.isSelected());
338:
339: urlField.setEnabled(inetShortcutRB.isSelected());
340: jLabel2.setEnabled(inetShortcutRB.isSelected());
341: }
342: }
|