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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.modules.profiler.ui.panels;
042:
043: import org.netbeans.lib.profiler.ui.components.HTMLTextArea;
044:
045: //import org.netbeans.api.progress.ProgressHandle;
046: //import org.netbeans.api.progress.ProgressHandleFactory;
047: import org.openide.util.NbBundle;
048: import org.openide.util.RequestProcessor;
049: import java.awt.Dimension;
050: import java.awt.GridBagConstraints;
051: import java.awt.GridBagLayout;
052: import java.util.ArrayList;
053: import java.util.Collections;
054: import java.util.Iterator;
055: import java.util.List;
056: import javax.swing.JButton;
057: import javax.swing.JLabel;
058: import javax.swing.JPanel;
059: import javax.swing.JScrollPane;
060: import javax.swing.SwingUtilities;
061:
062: /**
063: *
064: * @author Jiri Sedlacek
065: */
066: public class PreferredInstrFilterPanel extends JPanel {
067: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
068:
069: public abstract static class FilterResolver {
070: //~ Instance fields ------------------------------------------------------------------------------------------------------
071:
072: private String[] filterValues;
073:
074: //~ Methods --------------------------------------------------------------------------------------------------------------
075:
076: public synchronized String[] getFilterValues() {
077: if (filterValues == null) {
078: filterValues = computeFilterValues();
079: }
080:
081: return filterValues;
082: }
083:
084: protected abstract String[] computeFilterValues();
085: }
086:
087: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
088:
089: // -----
090: // I18N String constants
091: private static final String TO_QUICKFILTER_STRING = NbBundle
092: .getMessage(PreferredInstrFilterPanel.class,
093: "PreferredInstrFilterPanel_ToQuickFilterString"); //NOI18N
094: private static final String TO_QUICKFILTER_ACCESS_DESCR = NbBundle
095: .getMessage(PreferredInstrFilterPanel.class,
096: "PreferredInstrFilterPanel_ToQuickFilterAccessDescr"); //NOI18N
097: private static final String CLOSE_BUTTON_STRING = NbBundle
098: .getMessage(PreferredInstrFilterPanel.class,
099: "PreferredInstrFilterPanel_CloseButtonString"); //NOI18N
100: private static final String PREFERRED_PACKAGES_CAPTION = NbBundle
101: .getMessage(PreferredInstrFilterPanel.class,
102: "PreferredInstrFilterPanel_PreferredPackagesCaption"); //NOI18N
103: private static final String COMPUTING_FILTER_STRING = NbBundle
104: .getMessage(PreferredInstrFilterPanel.class,
105: "PreferredInstrFilterPanel_ComputingFilterString"); //NOI18N
106: // -----
107: private static PreferredInstrFilterPanel defaultInstance;
108:
109: //~ Instance fields ----------------------------------------------------------------------------------------------------------
110:
111: public JButton CLOSE_BUTTON;
112: public JButton OPEN_IN_QUICKFILTER_BUTTON;
113: private HTMLTextArea packagesArea;
114: private JLabel label;
115:
116: //~ Constructors -------------------------------------------------------------------------------------------------------------
117:
118: /** Creates a new instance of PreferredInstrFilterPanel */
119: private PreferredInstrFilterPanel() {
120: initComponents();
121: }
122:
123: //~ Methods ------------------------------------------------------------------------------------------------------------------
124:
125: public static PreferredInstrFilterPanel getDefault() {
126: if (defaultInstance == null) {
127: defaultInstance = new PreferredInstrFilterPanel();
128: }
129:
130: return defaultInstance;
131: }
132:
133: public void init(final FilterResolver filterComputer) {
134: // Update busy UI from AWT
135: SwingUtilities.invokeLater(new Runnable() {
136: public void run() {
137: OPEN_IN_QUICKFILTER_BUTTON.setEnabled(false);
138: packagesArea.setText(COMPUTING_FILTER_STRING);
139:
140: // Compute filters in worker thread
141: RequestProcessor.getDefault().post(new Runnable() {
142: public void run() {
143: // ProgressHandle pHandle = null;
144: // try {
145: // pHandle = ProgressHandleFactory.createHandle(COMPUTING_FILTER_STRING);
146: // pHandle.setInitialDelay(0);
147: // pHandle.start();
148:
149: // Compute filter value
150: String[] packageNames = filterComputer
151: .getFilterValues();
152:
153: // Sort filter
154: List<String> packageNamesList = new ArrayList(
155: packageNames.length);
156:
157: for (String filterPart : packageNames) {
158: packageNamesList.add(filterPart);
159: }
160:
161: Collections.sort(packageNamesList);
162:
163: // Format: each package to separate line
164: final StringBuffer packageNamesBuffer = new StringBuffer(
165: packageNames.length);
166: Iterator<String> it = packageNamesList
167: .iterator();
168:
169: while (it.hasNext()) {
170: packageNamesBuffer.append(it.next());
171:
172: if (it.hasNext()) {
173: packageNamesBuffer.append("<br>"); // NOI18N
174: }
175: }
176:
177: // Update finished UI from AWT
178: SwingUtilities.invokeLater(new Runnable() {
179: public void run() {
180: packagesArea.setText(packageNamesBuffer
181: .toString());
182: packagesArea.setCaretPosition(0);
183: OPEN_IN_QUICKFILTER_BUTTON
184: .setEnabled(true);
185: }
186: });
187:
188: // } finally {
189: // if (pHandle != null) pHandle.finish();
190: // }
191: }
192: });
193: }
194: });
195: }
196:
197: private void initComponents() {
198: OPEN_IN_QUICKFILTER_BUTTON = new JButton();
199: org.openide.awt.Mnemonics.setLocalizedText(
200: OPEN_IN_QUICKFILTER_BUTTON, TO_QUICKFILTER_STRING);
201: OPEN_IN_QUICKFILTER_BUTTON.getAccessibleContext()
202: .setAccessibleDescription(TO_QUICKFILTER_ACCESS_DESCR);
203: CLOSE_BUTTON = new JButton();
204: org.openide.awt.Mnemonics.setLocalizedText(CLOSE_BUTTON,
205: CLOSE_BUTTON_STRING);
206:
207: label = new JLabel();
208: packagesArea = new HTMLTextArea();
209:
210: GridBagConstraints gridBagConstraints;
211:
212: setLayout(new GridBagLayout());
213:
214: label.setLabelFor(packagesArea);
215: org.openide.awt.Mnemonics.setLocalizedText(label,
216: PREFERRED_PACKAGES_CAPTION);
217: gridBagConstraints = new GridBagConstraints();
218: gridBagConstraints.gridx = 0;
219: gridBagConstraints.gridy = 0;
220: gridBagConstraints.insets = new java.awt.Insets(15, 10, 5, 10);
221: gridBagConstraints.anchor = GridBagConstraints.WEST;
222: add(label, gridBagConstraints);
223:
224: JScrollPane packagesAreaScrollPane = new JScrollPane();
225: packagesAreaScrollPane
226: .setPreferredSize(new Dimension(350, 200));
227: packagesArea.setText(""); //NOI18N
228: packagesAreaScrollPane.setViewportView(packagesArea);
229:
230: gridBagConstraints = new GridBagConstraints();
231: gridBagConstraints.gridx = 0;
232: gridBagConstraints.gridy = 1;
233: gridBagConstraints.weightx = 1;
234: gridBagConstraints.weighty = 1;
235: gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 10);
236: gridBagConstraints.fill = GridBagConstraints.BOTH;
237: gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
238: add(packagesAreaScrollPane, gridBagConstraints);
239: }
240: }
|