001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.dialog;
051:
052: import java.awt.event.ActionEvent;
053: import java.awt.event.ActionListener;
054: import java.util.Iterator;
055: import java.util.LinkedHashMap;
056: import java.util.Map;
057:
058: import javax.swing.AbstractAction;
059: import javax.swing.DefaultListSelectionModel;
060: import javax.swing.JButton;
061: import javax.swing.JComboBox;
062: import javax.swing.JComponent;
063: import javax.swing.JLabel;
064: import javax.swing.JList;
065: import javax.swing.JScrollPane;
066: import javax.swing.JTextField;
067: import javax.swing.plaf.basic.BasicListUI;
068:
069: import com.jgoodies.forms.builder.DefaultFormBuilder;
070: import com.jgoodies.forms.layout.CellConstraints;
071: import com.jgoodies.forms.layout.FormLayout;
072: import com.projity.field.Field;
073: import com.projity.pm.graphic.frames.MainFrame;
074: import com.projity.session.Session;
075: import com.projity.session.SessionFactory;
076: import com.projity.strings.Messages;
077: import com.projity.util.Alert;
078: import com.projity.util.Environment;
079:
080: public final class LookupDialog extends AbstractDialog {
081: private static final long serialVersionUID = 1L;
082: // use property utils to copy to project like struts
083: Field field;
084: JComboBox types;
085: JList results;
086: JScrollPane resultsPane;
087: JTextField match;
088: JButton find;
089: JButton removeButton;
090: LinkedHashMap resultMap;
091: String key;
092: String value;
093:
094: public static String[] getKeyAndValue(Field f) {
095: LookupDialog dlg = new LookupDialog(f);
096: dlg.initControls();
097: if (dlg.doModal()) {
098: return new String[] { dlg.key, dlg.value };
099: }
100: return null;
101:
102: }
103:
104: public ButtonPanel createButtonPanel() {
105: AbstractAction action = new AbstractAction(Messages
106: .getString("Text.Remove")) { //$NON-NLS-1$
107: public void actionPerformed(ActionEvent e) {
108: remove();
109: }
110: };
111: removeButton = new JButton(action);
112:
113: createOkCancelButtons();
114: ButtonPanel buttonPanel = new ButtonPanel();
115: buttonPanel.addButton(removeButton);
116: buttonPanel.addButton(ok);
117: buttonPanel.addButton(cancel);
118: return buttonPanel;
119: }
120:
121: private void remove() {
122: value = null;
123: key = null;
124: super .onOk();
125: }
126:
127: @Override
128: public void onOk() {
129: int index = results.getSelectedIndex();
130: if (index != -1) {
131: value = (String) results.getSelectedValue();
132: key = keyForValue(value);
133: }
134: super .onOk();
135: }
136:
137: private String keyForValue(String value) {
138: Iterator i = resultMap.entrySet().iterator();
139: while (i.hasNext()) {
140: Map.Entry entry = (Map.Entry) i.next();
141: if (value == entry.getValue())
142: return (String) entry.getKey();
143: }
144: return null;
145: }
146:
147: private LookupDialog(Field field) {
148: super (MainFrame.getInstance(), Messages
149: .getString("LookupDialog.LookupAnObject"), true); //$NON-NLS-1$
150: this .field = field;
151: }
152:
153: protected boolean initialOkEnabledState() {
154: return false;
155: }
156:
157: /**
158: * Creates, intializes and configures the UI components. Real applications
159: * may further bind the components to underlying models.
160: */
161: protected void initControls() {
162: types = new JComboBox(field.getLookupTypes().split(";")); //$NON-NLS-1$
163: results = new JList();
164: results
165: .setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
166: // results.setVisibleRowCount(15);
167: resultsPane = new JScrollPane(results);
168: if (Environment.isNewLook()) // The PLAF can override the custom renderer. This avoids that
169: results.setUI(new BasicListUI());
170:
171: match = new JTextField();
172: match.setToolTipText(Messages
173: .getString("LookupDialog.EnterPartOfTheName")); //$NON-NLS-1$
174: find = new JButton(Messages.getString("LookupDialog.Find")); //$NON-NLS-1$
175: find.addActionListener(new ActionListener() {
176:
177: public void actionPerformed(ActionEvent e) {
178: Session session = SessionFactory.getInstance()
179: .getSession(false);
180: if (session != null)
181: try {
182: resultMap = (LinkedHashMap) SessionFactory
183: .call(
184: session,
185: "queryLike",
186: new Class[] { String.class,
187: String.class },
188: new Object[] {
189: types.getSelectedItem(),
190: match.getText() });
191: results = new ActionJList(resultMap.values()
192: .toArray());
193: ((ActionJList) results)
194: .addActionListener(new ActionListener() {
195: public void actionPerformed(
196: ActionEvent e) {
197: onOk();
198: }
199: });
200: if (resultMap.isEmpty()) {
201: resultsPane
202: .getViewport()
203: .add(
204: new JLabel(
205: Messages
206: .getString("LookupDialog.NoMatchesFound"))); //$NON-NLS-1$
207: resultsPane.setEnabled(false);
208: ok.setEnabled(false);
209: } else {
210: resultsPane.setEnabled(true);
211: resultsPane.getViewport().add(results);
212: ok.setEnabled(true);
213: }
214: } catch (Exception e1) {
215: Alert
216: .error(Messages
217: .getString("LookupDialog.UnableToContactServer")); //$NON-NLS-1$
218: // TODO Auto-generated catch block
219: e1.printStackTrace();
220: }
221: // TODO Auto-generated method stub
222:
223: }
224: });
225: }
226:
227: // Component Creation and Initialization **********************************
228:
229: // Building *************************************************************
230:
231: /**
232: * Builds the panel. Initializes and configures components first, then
233: * creates a FormLayout, configures the layout, creates a builder, sets a
234: * border, and finally adds the components.
235: *
236: * @return the built panel
237: */
238:
239: public JComponent createContentPanel() {
240: // Separating the component initialization and configuration
241: // from the layout code makes both parts easier to read.
242: initControls();
243: //TODO set minimum size
244: FormLayout layout = new FormLayout(
245: "p, 3dlu, p,20dlu,p,3dlu,160dlu:grow,3dlu,p", // cols //$NON-NLS-1$
246: "p, 3dlu, p,3dlu,fill:default:grow"); // rows //$NON-NLS-1$
247:
248: // Create a builder that assists in adding components to the container.
249: // Wrap the panel with a standardized border.
250: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
251: builder.setDefaultDialogBorder();
252: CellConstraints cc = new CellConstraints();
253: builder.append(Messages.getString("LookupDialog.Type"), types); //$NON-NLS-1$
254: builder.append(
255: Messages.getString("LookupDialog.Find") + ":", match); //$NON-NLS-1$
256: builder.append(find);
257: builder.nextLine(2);
258: builder.append(Messages.getString("LookupDialog.Results")); //$NON-NLS-1$
259: builder.nextLine(2);
260: builder.add(resultsPane, cc.xyw(builder.getColumn(), builder
261: .getRow(), 9));
262: return builder.getPanel();
263: }
264:
265: }
|