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.midp.analyzer;
043:
044: import org.netbeans.modules.vmd.api.model.DesignComponent;
045: import org.netbeans.modules.vmd.api.model.DesignDocument;
046: import org.netbeans.modules.vmd.api.model.Debug;
047: import org.netbeans.modules.vmd.api.model.presenters.InfoPresenter;
048: import org.netbeans.modules.vmd.api.model.presenters.actions.DeleteSupport;
049: import org.netbeans.modules.vmd.midp.components.resources.ResourceCD;
050: import org.openide.util.NbBundle;
051:
052: import javax.swing.*;
053: import java.awt.*;
054: import java.util.*;
055: import java.util.List;
056:
057: /**
058: *
059: * @author Anton Chechel
060: */
061: public class ResourcesAnalyzerPanel extends javax.swing.JPanel {
062:
063: private DesignDocument document;
064: private Map<Long, String> resourceNames;
065: private Map<Long, Icon> resourceIcons;
066: private List<Long> resourceIDs;
067: private Icon resourceIcon = new ImageIcon(ResourceCD.ICON_PATH);
068:
069: ResourcesAnalyzerPanel() {
070: initComponents();
071: resourceIDs = new ArrayList<Long>();
072: resourceNames = new HashMap<Long, String>();
073: resourceIcons = new HashMap<Long, Icon>();
074: resourcesList.setCellRenderer(new ResourcesListRenderer());
075: }
076:
077: void setUnusedResources(DesignDocument document,
078: List<DesignComponent> resources) {
079: Collections.sort(resources, new Comparator<DesignComponent>() {
080: public int compare(DesignComponent c1, DesignComponent c2) {
081: int i = c1.getType().toString().compareToIgnoreCase(
082: c2.getType().toString());
083: if (i != 0)
084: return i;
085: String s1 = InfoPresenter.getDisplayName(c1);
086: String s2 = InfoPresenter.getDisplayName(c2);
087: if (s1 != null) {
088: i = s1.compareToIgnoreCase(s2);
089: if (i != 0)
090: return i;
091: return s1.compareTo(s2);
092: } else
093: return s2 != null ? 1 : 0;
094: }
095: });
096: // do not change list if the resource are equal
097: if (resources.size() == resourceIDs.size()) {
098: for (int i = 0; i < resources.size(); i++) {
099: if (resources.get(i).getComponentID() == resourceIDs
100: .get(i)) {
101: return;
102: }
103: }
104: }
105:
106: resourceIDs.clear();
107: resourceNames.clear();
108: resourceIcons.clear();
109: this .document = document;
110: ((DefaultListModel) resourcesList.getModel())
111: .removeAllElements();
112:
113: if (resources.isEmpty()) {
114: ((DefaultListModel) resourcesList.getModel())
115: .addElement(NbBundle.getMessage(
116: ResourcesAnalyzerPanel.class,
117: "ResourcesAnalyzer.nothing-found")); // NOI18N
118: resourcesList.clearSelection();
119: } else {
120: for (DesignComponent resource : resources) {
121: resourceIDs.add(resource.getComponentID());
122:
123: InfoPresenter info = resource
124: .getPresenter(InfoPresenter.class);
125: String resourceName;
126: Image image;
127: if (info != null) {
128: resourceName = info
129: .getDisplayName(InfoPresenter.NameType.PRIMARY);
130: image = info
131: .getIcon(InfoPresenter.IconType.COLOR_16x16);
132: } else {
133: Debug
134: .warning("Missing InfoPresenter for",
135: resource); // NOI18N
136: resourceName = NbBundle.getMessage(
137: ResourcesAnalyzerPanel.class,
138: "ResourcesAnalyzer.no-label"); // NOI18N
139: image = null;
140: }
141:
142: resourceNames.put(resource.getComponentID(),
143: resourceName);
144: resourceIcons.put(resource.getComponentID(),
145: image != null ? new ImageIcon(image)
146: : this .resourceIcon);
147:
148: ((DefaultListModel) resourcesList.getModel())
149: .addElement(resource.getComponentID());
150: }
151:
152: int size = resourcesList.getModel().getSize();
153: if (size > 0) {
154: resourcesList.setSelectionInterval(0, size - 1);
155: }
156: }
157:
158: }
159:
160: private void removeUnusedResources(final Object[] selectedElements) {
161: document.getTransactionManager().writeAccess(new Runnable() {
162: public void run() {
163: for (Object selected : selectedElements) {
164: if (!(selected instanceof Long))
165: continue;
166: DesignComponent resource = document
167: .getComponentByUID((Long) selected);
168: if (resource != null)
169: DeleteSupport.invokeDirectUserDeletion(
170: document, Collections
171: .singleton(resource), false);
172: ((DefaultListModel) resourcesList.getModel())
173: .removeElement(selected);
174: }
175: }
176: });
177: if (resourcesList.getModel().getSize() == 0)
178: ((DefaultListModel) resourcesList.getModel())
179: .addElement(NbBundle.getMessage(
180: ResourcesAnalyzerPanel.class,
181: "ResourcesAnalyzer.nothing-found")); // NOI18N
182: }
183:
184: private class ResourcesListRenderer extends DefaultListCellRenderer {
185:
186: public Component getListCellRendererComponent(JList list,
187: Object value, int index, boolean isSelected,
188: boolean cellHasFocus) {
189: final JLabel renderer = (JLabel) super
190: .getListCellRendererComponent(list, value, index,
191: isSelected, cellHasFocus);
192: if (document != null && value instanceof Long) {
193: renderer.setText(resourceNames.get((Long) value));
194: renderer.setIcon(resourceIcons.get((Long) value));
195: }
196: return renderer;
197: }
198:
199: }
200:
201: /** This method is called from within the constructor to
202: * initialize the form.
203: * WARNING: Do NOT modify this code. The content of this method is
204: * always regenerated by the Form Editor.
205: */
206: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
207: private void initComponents() {
208:
209: removeButton = new javax.swing.JButton();
210: jScrollPane1 = new javax.swing.JScrollPane();
211: resourcesList = new javax.swing.JList();
212:
213: setPreferredSize(new java.awt.Dimension(400, 150));
214:
215: org.openide.awt.Mnemonics.setLocalizedText(removeButton,
216: org.openide.util.NbBundle.getMessage(
217: ResourcesAnalyzerPanel.class,
218: "ResourcesAnalyzerPanel.removeButton.text")); // NOI18N
219: removeButton
220: .addActionListener(new java.awt.event.ActionListener() {
221: public void actionPerformed(
222: java.awt.event.ActionEvent evt) {
223: removeButtonActionPerformed(evt);
224: }
225: });
226:
227: resourcesList.setModel(new DefaultListModel());
228: jScrollPane1.setViewportView(resourcesList);
229:
230: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
231: this );
232: this .setLayout(layout);
233: layout.setHorizontalGroup(layout.createParallelGroup(
234: org.jdesktop.layout.GroupLayout.LEADING).add(
235: org.jdesktop.layout.GroupLayout.TRAILING,
236: layout.createSequentialGroup().add(jScrollPane1,
237: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
238: 279, Short.MAX_VALUE).addPreferredGap(
239: org.jdesktop.layout.LayoutStyle.RELATED).add(
240: removeButton)));
241: layout.setVerticalGroup(layout.createParallelGroup(
242: org.jdesktop.layout.GroupLayout.LEADING).add(
243: layout.createSequentialGroup().add(removeButton)
244: .addContainerGap(127, Short.MAX_VALUE)).add(
245: jScrollPane1,
246: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 150,
247: Short.MAX_VALUE));
248: }// </editor-fold>//GEN-END:initComponents
249:
250: private void removeButtonActionPerformed(
251: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
252: removeUnusedResources(resourcesList.getSelectedValues());
253: }//GEN-LAST:event_removeButtonActionPerformed
254:
255: // Variables declaration - do not modify//GEN-BEGIN:variables
256: private javax.swing.JScrollPane jScrollPane1;
257: private javax.swing.JButton removeButton;
258: private javax.swing.JList resourcesList;
259: // End of variables declaration//GEN-END:variables
260:
261: }
|