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.vmd.game.nbdialog;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Component;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.util.ArrayList;
049: import java.util.Collection;
050: import java.util.List;
051: import javax.swing.DefaultListCellRenderer;
052: import javax.swing.DefaultListModel;
053: import javax.swing.Icon;
054: import javax.swing.ImageIcon;
055: import javax.swing.JList;
056: import javax.swing.event.ListSelectionEvent;
057: import javax.swing.event.ListSelectionListener;
058: import org.netbeans.modules.vmd.game.dialog.PartialImageGridPreview;
059: import org.openide.DialogDescriptor;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062: import org.openide.util.HelpCtx;
063: import org.openide.util.NbBundle;
064: import org.openide.util.Utilities;
065:
066: /**
067: *
068: * @author kherink
069: */
070: public class SelectImageForLayerDialog extends javax.swing.JPanel {
071:
072: public static final boolean DEBUG = false;
073:
074: private static final Icon ICON_ERROR = new ImageIcon(
075: Utilities
076: .loadImage("org/netbeans/modules/vmd/midp/resources/error.gif")); // NOI18N
077:
078: private String path;
079: private Collection<FileObject> images;
080: private DialogDescriptor dd;
081: private PartialImageGridPreview imagePreview = new PartialImageGridPreview();
082:
083: private FileObject imgFile;
084:
085: public SelectImageForLayerDialog(String missingImagePath,
086: Collection<FileObject> images) {
087: this .path = missingImagePath;
088: this .images = images;
089: initComponents();
090: init();
091: }
092:
093: /** This method is called from within the constructor to
094: * initialize the form.
095: * WARNING: Do NOT modify this code. The content of this method is
096: * always regenerated by the Form Editor.
097: */
098: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
099: private void initComponents() {
100:
101: buttonGroupLayers = new javax.swing.ButtonGroup();
102: panelCustomizer = new javax.swing.JPanel();
103: labelImageFile = new javax.swing.JLabel();
104: jScrollPane1 = new javax.swing.JScrollPane();
105: listImageFileName = new javax.swing.JList();
106: panelPreview = new javax.swing.JPanel();
107: labelImagePreview = new javax.swing.JLabel();
108: panelImage = new javax.swing.JPanel();
109: panelError = new javax.swing.JPanel();
110: labelError = new javax.swing.JLabel();
111: jSeparator1 = new javax.swing.JSeparator();
112: jScrollPane2 = new javax.swing.JScrollPane();
113: textAreaReason = new javax.swing.JTextArea();
114:
115: labelImageFile.setText(org.openide.util.NbBundle.getMessage(
116: SelectImageForLayerDialog.class,
117: "SelectImageForLayerDialog.labelSelectImg.txt")); // NOI18N
118:
119: listImageFileName.setModel(this .getImageListModel());
120: listImageFileName
121: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
122: jScrollPane1.setViewportView(listImageFileName);
123:
124: org.jdesktop.layout.GroupLayout panelCustomizerLayout = new org.jdesktop.layout.GroupLayout(
125: panelCustomizer);
126: panelCustomizer.setLayout(panelCustomizerLayout);
127: panelCustomizerLayout.setHorizontalGroup(panelCustomizerLayout
128: .createParallelGroup(
129: org.jdesktop.layout.GroupLayout.LEADING).add(
130: panelCustomizerLayout.createSequentialGroup()
131: .add(labelImageFile).addContainerGap(
132: 224, Short.MAX_VALUE)).add(
133: jScrollPane1,
134: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
135: 305, Short.MAX_VALUE));
136: panelCustomizerLayout
137: .setVerticalGroup(panelCustomizerLayout
138: .createParallelGroup(
139: org.jdesktop.layout.GroupLayout.LEADING)
140: .add(
141: panelCustomizerLayout
142: .createSequentialGroup()
143: .add(labelImageFile)
144: .addPreferredGap(
145: org.jdesktop.layout.LayoutStyle.RELATED)
146: .add(
147: jScrollPane1,
148: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
149: 203, Short.MAX_VALUE)));
150:
151: labelImagePreview.setText(org.openide.util.NbBundle.getMessage(
152: SelectImageForLayerDialog.class,
153: "SelectImageForLayerDialog.labelImgPreview.txt")); // NOI18N
154:
155: panelImage.setBorder(javax.swing.BorderFactory
156: .createLineBorder(new java.awt.Color(102, 102, 102)));
157: this .panelImage.add(this .imagePreview, BorderLayout.CENTER);
158: panelImage.setLayout(new java.awt.BorderLayout());
159:
160: org.jdesktop.layout.GroupLayout panelPreviewLayout = new org.jdesktop.layout.GroupLayout(
161: panelPreview);
162: panelPreview.setLayout(panelPreviewLayout);
163: panelPreviewLayout
164: .setHorizontalGroup(panelPreviewLayout
165: .createParallelGroup(
166: org.jdesktop.layout.GroupLayout.LEADING)
167: .add(
168: panelPreviewLayout
169: .createSequentialGroup()
170: .addContainerGap()
171: .add(
172: panelPreviewLayout
173: .createParallelGroup(
174: org.jdesktop.layout.GroupLayout.LEADING)
175: .add(
176: panelImage,
177: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
178: 301,
179: Short.MAX_VALUE)
180: .add(
181: panelPreviewLayout
182: .createSequentialGroup()
183: .add(
184: labelImagePreview)
185: .addContainerGap(
186: 207,
187: Short.MAX_VALUE)))));
188: panelPreviewLayout
189: .setVerticalGroup(panelPreviewLayout
190: .createParallelGroup(
191: org.jdesktop.layout.GroupLayout.LEADING)
192: .add(
193: panelPreviewLayout
194: .createSequentialGroup()
195: .add(labelImagePreview)
196: .addPreferredGap(
197: org.jdesktop.layout.LayoutStyle.RELATED)
198: .add(
199: panelImage,
200: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
201: 203, Short.MAX_VALUE)));
202:
203: labelError.setForeground(new java.awt.Color(255, 0, 0));
204:
205: org.jdesktop.layout.GroupLayout panelErrorLayout = new org.jdesktop.layout.GroupLayout(
206: panelError);
207: panelError.setLayout(panelErrorLayout);
208: panelErrorLayout.setHorizontalGroup(panelErrorLayout
209: .createParallelGroup(
210: org.jdesktop.layout.GroupLayout.LEADING).add(
211: org.jdesktop.layout.GroupLayout.TRAILING,
212: labelError,
213: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
214: 624, Short.MAX_VALUE));
215: panelErrorLayout
216: .setVerticalGroup(panelErrorLayout
217: .createParallelGroup(
218: org.jdesktop.layout.GroupLayout.LEADING)
219: .add(
220: panelErrorLayout
221: .createSequentialGroup()
222: .addContainerGap(40,
223: Short.MAX_VALUE)
224: .add(
225: labelError,
226: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
227: 27,
228: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)));
229:
230: jScrollPane2.setBorder(javax.swing.BorderFactory
231: .createLineBorder(new java.awt.Color(102, 102, 102)));
232:
233: textAreaReason.setColumns(20);
234: textAreaReason.setRows(3);
235: textAreaReason.setTabSize(4);
236: jScrollPane2.setViewportView(textAreaReason);
237:
238: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
239: this );
240: this .setLayout(layout);
241: layout
242: .setHorizontalGroup(layout
243: .createParallelGroup(
244: org.jdesktop.layout.GroupLayout.LEADING)
245: .add(
246: layout
247: .createSequentialGroup()
248: .addContainerGap()
249: .add(
250: layout
251: .createParallelGroup(
252: org.jdesktop.layout.GroupLayout.LEADING)
253: .add(
254: org.jdesktop.layout.GroupLayout.TRAILING,
255: jSeparator1,
256: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
257: 624,
258: Short.MAX_VALUE)
259: .add(
260: jScrollPane2,
261: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
262: 624,
263: Short.MAX_VALUE)
264: .add(
265: org.jdesktop.layout.GroupLayout.TRAILING,
266: panelError,
267: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
268: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
269: Short.MAX_VALUE)
270: .add(
271: org.jdesktop.layout.GroupLayout.TRAILING,
272: layout
273: .createSequentialGroup()
274: .add(
275: panelCustomizer,
276: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
277: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
278: Short.MAX_VALUE)
279: .addPreferredGap(
280: org.jdesktop.layout.LayoutStyle.RELATED)
281: .add(
282: panelPreview,
283: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
284: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
285: Short.MAX_VALUE)))
286: .addContainerGap()));
287: layout
288: .setVerticalGroup(layout
289: .createParallelGroup(
290: org.jdesktop.layout.GroupLayout.LEADING)
291: .add(
292: layout
293: .createSequentialGroup()
294: .addContainerGap()
295: .add(
296: jScrollPane2,
297: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
298: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
299: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
300: .addPreferredGap(
301: org.jdesktop.layout.LayoutStyle.RELATED)
302: .add(
303: jSeparator1,
304: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
305: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
306: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
307: .addPreferredGap(
308: org.jdesktop.layout.LayoutStyle.RELATED)
309: .add(
310: layout
311: .createParallelGroup(
312: org.jdesktop.layout.GroupLayout.TRAILING)
313: .add(
314: panelPreview,
315: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
316: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
317: Short.MAX_VALUE)
318: .add(
319: panelCustomizer,
320: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
321: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
322: Short.MAX_VALUE))
323: .addPreferredGap(
324: org.jdesktop.layout.LayoutStyle.RELATED)
325: .add(
326: panelError,
327: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
328: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
329: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)));
330: }// </editor-fold>//GEN-END:initComponents
331:
332: // Variables declaration - do not modify//GEN-BEGIN:variables
333: private javax.swing.ButtonGroup buttonGroupLayers;
334: private javax.swing.JScrollPane jScrollPane1;
335: private javax.swing.JScrollPane jScrollPane2;
336: private javax.swing.JSeparator jSeparator1;
337: private javax.swing.JLabel labelError;
338: private javax.swing.JLabel labelImageFile;
339: private javax.swing.JLabel labelImagePreview;
340: private javax.swing.JList listImageFileName;
341: private javax.swing.JPanel panelCustomizer;
342: private javax.swing.JPanel panelError;
343: private javax.swing.JPanel panelImage;
344: private javax.swing.JPanel panelPreview;
345: private javax.swing.JTextArea textAreaReason;
346:
347: // End of variables declaration//GEN-END:variables
348:
349: public void setDialogDescriptor(DialogDescriptor dd) {
350: this .dd = dd;
351: }
352:
353: private void init() {
354: HelpCtx
355: .setHelpIDString(this ,
356: "org.netbeans.modules.vmd.game.nbdialog.SelectImageForLayerDialog");
357: this .labelError.setText(NbBundle.getMessage(
358: SelectImageForLayerDialog.class,
359: "SelectImageForLayerDialog.labelSelectImgDesc.txt"));
360:
361: this .textAreaReason.setBackground(this .getBackground());
362: this .textAreaReason.setText(path);
363:
364: this .labelError.setIcon(ICON_ERROR);
365: this .panelImage.add(this .imagePreview, BorderLayout.CENTER);
366:
367: this .listImageFileName
368: .addListSelectionListener(new ImageListListener());
369: this .listImageFileName
370: .setCellRenderer(new DefaultListCellRenderer() {
371: public Component getListCellRendererComponent(
372: JList src, Object value, int index,
373: boolean isSelected, boolean hasfocus) {
374: FileObject entry = (FileObject) value;
375: return super .getListCellRendererComponent(src,
376: FileUtil.getFileDisplayName(entry),
377: index, isSelected, hasfocus);
378: }
379: });
380:
381: }
382:
383: private List<FileObject> getImageList() {
384: List<FileObject> list = new ArrayList<FileObject>();
385: list.addAll(this .images);
386: return list;
387: }
388:
389: private DefaultListModel getImageListModel() {
390: DefaultListModel dlm = new DefaultListModel();
391: List<FileObject> images = this .getImageList();
392: for (FileObject imageEntry : images) {
393: dlm.addElement(imageEntry);
394: }
395: return dlm;
396: }
397:
398: public void setOKButtonEnabled(boolean enable) {
399: if (!enable) {
400: this .labelError.setIcon(ICON_ERROR);
401: } else {
402: this .labelError.setIcon(null);
403: }
404: this .dd.setValid(enable);
405: }
406:
407: private class ImageListListener implements ListSelectionListener {
408:
409: public void valueChanged(ListSelectionEvent e) {
410: if (e.getValueIsAdjusting())
411: return;
412: this .handleImageSelectionChange();
413: }
414:
415: private void handleImageSelectionChange() {
416: try {
417: loadImagePreview();
418: labelError.setText(null);
419: setOKButtonEnabled(true);
420: FileObject entry = (FileObject) listImageFileName
421: .getSelectedValue();
422: imgFile = entry;
423: } catch (MalformedURLException e) {
424: setOKButtonEnabled(false);
425: labelError
426: .setText(NbBundle
427: .getMessage(
428: SelectImageForLayerDialog.class,
429: "SelectImageForLayerDialog.labelInvalidImgLoc.txt"));
430: e.printStackTrace();
431: } catch (IllegalArgumentException iae) {
432: setOKButtonEnabled(false);
433: labelError
434: .setText(NbBundle
435: .getMessage(
436: SelectImageForLayerDialog.class,
437: "SelectImageForLayerDialog.labelInvalidImgFomat.txt"));
438: iae.printStackTrace();
439: }
440: }
441: }
442:
443: private void loadImagePreview() throws MalformedURLException,
444: IllegalArgumentException {
445: if (DEBUG)
446: System.out.println("load image preview"); // NOI18N
447:
448: FileObject entry = (FileObject) this .listImageFileName
449: .getSelectedValue();
450: URL imageURL = null;
451: try {
452: imageURL = entry.getURL();
453: } catch (Exception e) {
454: e.printStackTrace();
455: }
456:
457: assert (imageURL != null);
458:
459: this .imagePreview.setImageURL(imageURL);
460: this .repaint();
461: }
462:
463: public FileObject getValue() {
464: return this.imgFile;
465: }
466:
467: }
|