001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.persistence.wizard.entity;
043:
044: import java.awt.Dimension;
045: import java.util.Set;
046: import javax.lang.model.element.TypeElement;
047: import javax.swing.Icon;
048: import javax.swing.ImageIcon;
049: import javax.swing.JLabel;
050: import javax.swing.SwingUtilities;
051: import javax.swing.event.ChangeListener;
052: import javax.swing.event.DocumentEvent;
053: import javax.swing.event.DocumentListener;
054: import org.netbeans.api.java.source.ClassIndex.NameKind;
055: import org.netbeans.api.java.source.ClassIndex.SearchScope;
056: import org.netbeans.api.java.source.ClasspathInfo;
057: import org.netbeans.api.java.source.ElementHandle;
058: import org.netbeans.api.java.source.ui.TypeElementFinder;
059: import org.netbeans.api.project.Project;
060: import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
061: import org.netbeans.modules.j2ee.persistence.wizard.Util;
062: import org.netbeans.modules.j2ee.persistence.wizard.unit.PersistenceUnitWizardPanel.TableGeneration;
063: import org.openide.util.Utilities;
064:
065: /**
066: *
067: * @author Martin Adamek
068: */
069: public class EntityWizardPanel extends javax.swing.JPanel {
070:
071: private Project project;
072: private ChangeListener listener;
073: private PersistenceUnit persistenceUnit;
074:
075: static final String IS_VALID = "EntityWizardPanel_isValid"; //NOI18N
076:
077: public EntityWizardPanel(ChangeListener changeListener) {
078: this .setProject(project);
079: this .listener = changeListener;
080: initComponents();
081:
082: setPersistenceUnitButtonVisibility(false, null);
083:
084: primaryKeyTextField.getDocument().addDocumentListener(
085: new DocumentListener() {
086: public void changedUpdate(DocumentEvent e) {
087: listener.stateChanged(null);
088: }
089:
090: public void insertUpdate(DocumentEvent e) {
091: listener.stateChanged(null);
092: }
093:
094: public void removeUpdate(DocumentEvent e) {
095: listener.stateChanged(null);
096: }
097: });
098:
099: }
100:
101: public String getPrimaryKeyClassName() {
102: return primaryKeyTextField.getText();
103: }
104:
105: void setPersistenceUnitButtonVisibility(boolean visible,
106: String warning) {
107: createPUButton.setVisible(visible);
108: Icon icon = null;
109: if (warning != null) {
110: icon = new ImageIcon(
111: Utilities
112: .loadImage("org/netbeans/modules/j2ee/persistence/ui/resources/warning.gif"));
113: } else {
114: warning = " ";
115: }
116: createPUWarningLabel.setIcon(icon);
117: createPUWarningLabel.setText(warning);
118: createPUWarningLabel.setToolTipText(warning);
119: }
120:
121: void setProject(Project project) {
122: this .project = project;
123: }
124:
125: public PersistenceUnit getPersistenceUnit() {
126: return persistenceUnit;
127: }
128:
129: /** This method is called from within the constructor to
130: * initialize the form.
131: * WARNING: Do NOT modify this code. The content of this method is
132: * always regenerated by the Form Editor.
133: */
134: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
135: private void initComponents() {
136:
137: persistenceGroup = new javax.swing.ButtonGroup();
138: accessTypeGroup = new javax.swing.ButtonGroup();
139: jLabel1 = new javax.swing.JLabel();
140: primaryKeyTextField = new javax.swing.JTextField();
141: createPUButton = new javax.swing.JButton();
142: searchButton = new javax.swing.JButton();
143: createPUWarningLabel = new ShyLabel();
144:
145: jLabel1
146: .setDisplayedMnemonic(java.util.ResourceBundle
147: .getBundle(
148: "org/netbeans/modules/j2ee/persistence/wizard/entity/Bundle")
149: .getString("MN_PrimaryKeyType").charAt(0));
150: jLabel1.setLabelFor(primaryKeyTextField);
151: jLabel1.setText(org.openide.util.NbBundle.getBundle(
152: EntityWizardPanel.class).getString(
153: "LBL_PrimaryKeyClass")); // NOI18N
154:
155: primaryKeyTextField.setText("Long");
156:
157: createPUButton
158: .setMnemonic(java.util.ResourceBundle
159: .getBundle(
160: "org/netbeans/modules/j2ee/persistence/wizard/entity/Bundle")
161: .getString("MN_CreatePersistenceUnit")
162: .charAt(0));
163: createPUButton.setText(org.openide.util.NbBundle.getMessage(
164: EntityWizardPanel.class, "LBL_CreatePersistenceUnit")); // NOI18N
165: createPUButton
166: .addActionListener(new java.awt.event.ActionListener() {
167: public void actionPerformed(
168: java.awt.event.ActionEvent evt) {
169: createPUButtonActionPerformed(evt);
170: }
171: });
172:
173: searchButton.setText("...");
174: searchButton
175: .addActionListener(new java.awt.event.ActionListener() {
176: public void actionPerformed(
177: java.awt.event.ActionEvent evt) {
178: searchButtonActionPerformed(evt);
179: }
180: });
181:
182: createPUWarningLabel.setText(" ");
183:
184: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
185: this );
186: this .setLayout(layout);
187: layout
188: .setHorizontalGroup(layout
189: .createParallelGroup(
190: org.jdesktop.layout.GroupLayout.LEADING)
191: .add(
192: layout
193: .createSequentialGroup()
194: .add(jLabel1)
195: .addPreferredGap(
196: org.jdesktop.layout.LayoutStyle.RELATED)
197: .add(
198: primaryKeyTextField,
199: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
200: 173, Short.MAX_VALUE)
201: .addPreferredGap(
202: org.jdesktop.layout.LayoutStyle.RELATED)
203: .add(searchButton)).add(
204: createPUButton).add(
205: createPUWarningLabel));
206: layout
207: .setVerticalGroup(layout
208: .createParallelGroup(
209: org.jdesktop.layout.GroupLayout.LEADING)
210: .add(
211: layout
212: .createSequentialGroup()
213: .add(
214: layout
215: .createParallelGroup(
216: org.jdesktop.layout.GroupLayout.BASELINE)
217: .add(jLabel1)
218: .add(
219: searchButton)
220: .add(
221: primaryKeyTextField,
222: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
223: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
224: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
225: .addPreferredGap(
226: org.jdesktop.layout.LayoutStyle.RELATED,
227: 30, Short.MAX_VALUE)
228: .add(createPUWarningLabel)
229: .addPreferredGap(
230: org.jdesktop.layout.LayoutStyle.RELATED)
231: .add(createPUButton)));
232:
233: primaryKeyTextField.getAccessibleContext()
234: .setAccessibleDescription(
235: org.openide.util.NbBundle.getMessage(
236: EntityWizardPanel.class,
237: "LBL_PrimaryKeyClass")); // NOI18N
238: createPUButton.getAccessibleContext().setAccessibleDescription(
239: org.openide.util.NbBundle.getMessage(
240: EntityWizardPanel.class,
241: "LBL_CreatePersistenceUnit")); // NOI18N
242: }// </editor-fold>//GEN-END:initComponents
243:
244: private void searchButtonActionPerformed(
245: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
246: SwingUtilities.invokeLater(new Runnable() {
247: public void run() {
248: final ElementHandle<TypeElement> handle = TypeElementFinder
249: .find(null, new TypeElementFinder.Customizer() {
250:
251: public Set<ElementHandle<TypeElement>> query(
252: ClasspathInfo classpathInfo,
253: String textForQuery,
254: NameKind nameKind,
255: Set<SearchScope> searchScopes) {
256: return classpathInfo.getClassIndex()
257: .getDeclaredTypes(textForQuery,
258: nameKind, searchScopes);
259: }
260:
261: public boolean accept(
262: ElementHandle<TypeElement> typeHandle) {
263: //XXX not all types are supported as identifiers by the jpa spec, but
264: // leaving unrestricted for now since different persistence providers
265: // might support more types
266: return true;
267: }
268: });
269:
270: if (handle != null) {
271: primaryKeyTextField.setText(handle
272: .getQualifiedName());
273: }
274: }
275: });
276: }//GEN-LAST:event_searchButtonActionPerformed
277:
278: private void createPUButtonActionPerformed(
279: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createPUButtonActionPerformed
280: persistenceUnit = Util.buildPersistenceUnitUsingWizard(project,
281: null, TableGeneration.CREATE);
282: if (persistenceUnit != null) {
283: firePropertyChange(IS_VALID, false, true);
284: setPersistenceUnitButtonVisibility(false, null);
285: }
286: }//GEN-LAST:event_createPUButtonActionPerformed
287:
288: // Variables declaration - do not modify//GEN-BEGIN:variables
289: private javax.swing.ButtonGroup accessTypeGroup;
290: private javax.swing.JButton createPUButton;
291: private javax.swing.JLabel createPUWarningLabel;
292: private javax.swing.JLabel jLabel1;
293: private javax.swing.ButtonGroup persistenceGroup;
294: private javax.swing.JTextField primaryKeyTextField;
295: private javax.swing.JButton searchButton;
296:
297: // End of variables declaration//GEN-END:variables
298:
299: /**
300: * A crude attempt at a label which doesn't expand its parent.
301: */
302: private static final class ShyLabel extends JLabel {
303:
304: @Override
305: public Dimension getPreferredSize() {
306: Dimension size = super .getPreferredSize();
307: size.width = 0;
308: return size;
309: }
310:
311: @Override
312: public Dimension getMinimumSize() {
313: Dimension size = super .getMinimumSize();
314: size.width = 0;
315: return size;
316: }
317: }
318: }
|