001: package org.airtodo.gui.panel_open;
002:
003: import static org.air.framework.gui.box.Gap.small;
004:
005: import java.awt.Component;
006: import java.awt.Dialog;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ItemEvent;
009: import java.awt.event.ItemListener;
010: import java.util.Map;
011:
012: import javax.swing.Action;
013: import javax.swing.Box;
014: import javax.swing.JFileChooser;
015: import javax.swing.JTextField;
016: import javax.swing.SwingUtilities;
017: import javax.swing.event.DocumentEvent;
018: import javax.swing.event.DocumentListener;
019:
020: import org.air.framework.core.GuiUtils;
021: import org.air.framework.core.Utils;
022: import org.air.framework.gui.actions.basic.BasicAbstractAction;
023: import org.air.framework.gui.actions.other.ActionLaunch;
024: import org.air.framework.gui.box.BxX;
025: import org.air.framework.gui.box.BxY;
026: import org.air.framework.gui.dialogs.input.AbstractOpenTabbedPanel;
027: import org.air.framework.gui.lookup.WidgetComboLookup;
028: import org.air.framework.gui.widgets.Button;
029: import org.air.framework.gui.widgets.RequiredLabel;
030: import org.air.framework.gui.widgets.WidgetTextArea;
031: import org.air.framework.gui.widgets.WidgetTextField;
032: import org.air.framework.i18n.NlsColumns;
033: import org.air.framework.i18n.NlsMessages;
034: import org.air.framework.properties.Icons;
035: import org.air.framework.records.Attachments;
036: import org.air.framework.records.Common;
037: import org.airtodo.LinkUtils;
038: import org.airtodo.db.types.AttachmentType;
039: import org.airtodo.gui.dialog_table.NoteGridNonSelectableDialog;
040: import org.airtodo.gui_common.actions.other.ActionNotes;
041: import org.airtodo.gui_common.components.lookup_data.LookupDataAttachment;
042:
043: public class AttachmentPanel extends AbstractOpenTabbedPanel {
044: public static enum NLS {
045: TITLE, //
046: BUTTON_CHOOSE_FILE, //
047: LABEL_RESOURCE
048: }
049:
050: private static final String NLS_TITLE = NlsMessages
051: .getString(NLS.TITLE);
052: private static final String NLS_BUTTON_CHOOSE_FILE = NlsMessages
053: .getString(NLS.BUTTON_CHOOSE_FILE);
054: private static final String NLS_LABEL_RESOURCE = NlsMessages
055: .getString(NLS.LABEL_RESOURCE);
056: //
057: private long masterId;
058: private long attachmentId;
059:
060: public AttachmentPanel() {
061: BxY bxY = new BxY(NLS_TITLE);
062: getTabGeneral().add(bxY);
063: bxY.add(getWidgetTextFieldName());
064: bxY.add(getWidgetAttachmentCombo());
065: bxY.add(new BxX(new RequiredLabel(NLS_LABEL_RESOURCE + ":",
066: true), Box.createHorizontalGlue()));
067: bxY.add(new BxX(getWidgetTextFieldAddress(), small(),
068: new Button(getActionBrowse())));
069: bxY.add(small());
070: bxY.add(new BxX(new Button(getActionLaunch()), Box
071: .createHorizontalGlue()));
072: getTabGeneral().add(small());
073: getTabGeneral().add(getWidgetTextAreaComment());
074: //
075: getButtonPanel().addAll(getButtonNotes(),
076: Box.createHorizontalGlue());
077:
078: endbleDisableBrowseButton();
079: }
080:
081: //
082: // getButtonNotes
083: //
084: private Button buttonNotes;
085:
086: private Component getButtonNotes() {
087: if (buttonNotes == null) {
088: buttonNotes = new Button(new ActionNotes() {
089: public void actionPerformed(ActionEvent e) {
090: Dialog owner = (Dialog) SwingUtilities
091: .windowForComponent(getOwner());
092: NoteGridNonSelectableDialog dialog = new NoteGridNonSelectableDialog(
093: owner, getWidgetTextFieldName().getText());
094: dialog.setMasterId(attachmentId);
095: dialog.loadAndSelect();
096: dialog.setVisible(true);
097: }
098: });
099: }
100: return buttonNotes;
101: }
102:
103: //
104: // getWidgetAttachmentCombo
105: //
106: private WidgetComboLookup widgetAttachmentCombo;
107:
108: private WidgetComboLookup getWidgetAttachmentCombo() {
109: if (widgetAttachmentCombo == null) {
110: widgetAttachmentCombo = new WidgetComboLookup(
111: new LookupDataAttachment());
112: widgetAttachmentCombo.getComboBox().addItemListener(
113: new ItemListener() {
114: public void itemStateChanged(ItemEvent e) {
115: endbleDisableBrowseButton();
116: }
117: });
118: }
119: return widgetAttachmentCombo;
120: }
121:
122: private void endbleDisableBrowseButton() {
123: int posFile = LookupDataAttachment.NLS.ATTACHMENT_TYPE_FILE
124: .ordinal();
125: AttachmentType attachmentType = (AttachmentType) getWidgetAttachmentCombo()
126: .getSelectedItem();
127: if (attachmentType != null) {
128: int pos = (attachmentType).getValue();
129: getActionBrowse().setEnabled(pos == posFile);
130: }
131: }
132:
133: //
134: // getWidgetTextFieldName
135: //
136: private WidgetTextField widgetTextFieldName;
137:
138: private WidgetTextField getWidgetTextFieldName() {
139: if (widgetTextFieldName == null)
140: widgetTextFieldName = new WidgetTextField(
141: Attachments.DESCRIPTION);
142: return widgetTextFieldName;
143: }
144:
145: //
146: // getWidgetTextFieldAddress
147: //
148: private JTextField widgetTextFieldAddress;
149:
150: private JTextField getWidgetTextFieldAddress() {
151: if (widgetTextFieldAddress == null) {
152: widgetTextFieldAddress = new JTextField();
153: widgetTextFieldAddress.getDocument().addDocumentListener(
154: new DocumentListener() {
155: public void changedUpdate(DocumentEvent e) {
156: // Nothing to do
157: }
158:
159: public void removeUpdate(DocumentEvent e) {
160: textUpdateEvent(e);
161: }
162:
163: public void insertUpdate(DocumentEvent e) {
164: textUpdateEvent(e);
165: }
166:
167: private void textUpdateEvent(DocumentEvent e) {
168: getActionLaunch().setEnabled(
169: e.getDocument().getLength() > 0);
170: }
171: });
172: }
173: return widgetTextFieldAddress;
174: }
175:
176: //
177: // getWidgetTextAreaComment
178: //
179: private WidgetTextArea widgetTextAreaComment;
180:
181: private WidgetTextArea getWidgetTextAreaComment() {
182: if (widgetTextAreaComment == null)
183: widgetTextAreaComment = new WidgetTextArea(
184: Attachments.COMMENT);
185: return widgetTextAreaComment;
186: }
187:
188: private BasicAbstractAction actionBrowse;
189:
190: private Action getActionBrowse() {
191: if (actionBrowse == null)
192: actionBrowse = new BasicAbstractAction() {
193: public void actionPerformed(ActionEvent e) {
194: JFileChooser fileChooser = new JFileChooser();
195: int returnVal = fileChooser
196: .showOpenDialog(AttachmentPanel.this );
197: if (returnVal == JFileChooser.APPROVE_OPTION)
198: getWidgetTextFieldAddress().setText(
199: fileChooser.getSelectedFile()
200: .getAbsolutePath());
201: }
202: };
203: actionBrowse.setName(NLS_BUTTON_CHOOSE_FILE
204: + BasicAbstractAction.ELLIPSIS);
205: actionBrowse.setSmallIcon(Icons.FILE_CHOOSER);
206: actionBrowse.setEnabled(false);
207: return actionBrowse;
208: }
209:
210: private BasicAbstractAction actionLaunch;
211:
212: private Action getActionLaunch() {
213: if (actionLaunch == null)
214: actionLaunch = new ActionLaunch() {
215: public void actionPerformed(ActionEvent e) {
216: Dialog owner = (Dialog) SwingUtilities
217: .windowForComponent(AttachmentPanel.this );
218: String text = getWidgetTextFieldAddress().getText();
219: AttachmentType type = (AttachmentType) getWidgetAttachmentCombo()
220: .getSelectedItem();
221: if (text != null)
222: LinkUtils.launch(owner, type, text);
223: }
224: };
225: return actionLaunch;
226: }
227:
228: //
229: // isFormValid
230: //
231: @Override
232: public boolean isFormValid() {
233: boolean b = getWidgetTextFieldAddress().getText().trim()
234: .length() > 0;
235: if (!b) {
236: Dialog owner = (Dialog) SwingUtilities
237: .windowForComponent(this );
238: GuiUtils.showErrorRequiredMessage(owner, NlsColumns
239: .getText(Attachments.NAME));
240: }
241: return b;
242: }
243:
244: @Override
245: public void fromGui(Map map) {
246: map.put(Utils.getColumnIdentifier(Attachments.MASTER_ID),
247: masterId);
248: getWidgetAttachmentCombo().fromGui(map); // !!!!!!!!!!!!!!!!!!!!!!!!! error
249: getWidgetTextFieldName().fromGui(map);
250: String text = getWidgetTextFieldAddress().getText();
251: map.put(Utils.getColumnIdentifier(Attachments.NAME), text);
252: getWidgetTextAreaComment().fromGui(map);
253: }
254:
255: @Override
256: public void toGui(Map map) {
257: masterId = (Long) map.get(Utils
258: .getColumnIdentifier(Attachments.MASTER_ID));
259: getWidgetAttachmentCombo().toGui(map);
260: getWidgetTextFieldName().toGui(map);
261: getWidgetTextFieldAddress().setText(
262: (String) map.get(Utils
263: .getColumnIdentifier(Attachments.NAME)));
264: getWidgetTextAreaComment().toGui(map);
265: //
266: attachmentId = (Long) map.get(Utils
267: .getColumnIdentifier(Common.ID));
268: }
269: }
|