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.ppoints;
042:
043: import org.netbeans.api.project.Project;
044: import org.netbeans.modules.profiler.ppoints.ui.LoadGeneratorCustomizer;
045: import org.netbeans.modules.profiler.ppoints.ui.ValidityAwarePanel;
046: import org.netbeans.modules.profiler.spi.LoadGenPlugin;
047: import org.openide.ErrorManager;
048: import org.openide.filesystems.FileUtil;
049: import org.openide.util.Lookup;
050: import org.openide.util.LookupEvent;
051: import org.openide.util.LookupListener;
052: import org.openide.util.NbBundle;
053: import org.openide.util.Utilities;
054: import org.openide.util.WeakListeners;
055: import java.beans.PropertyChangeEvent;
056: import java.io.File;
057: import java.text.MessageFormat;
058: import java.util.Properties;
059: import javax.swing.Icon;
060: import javax.swing.ImageIcon;
061:
062: /**
063: *
064: * @author Jaroslav Bachorik
065: */
066: public class LoadGenProfilingPointFactory extends
067: CodeProfilingPointFactory {
068: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
069:
070: // -----
071: // I18N String constants
072: private static final String PP_TYPE = NbBundle.getMessage(
073: LoadGenProfilingPointFactory.class,
074: "LoadGenProfilingPointFactory_PpType"); // NOI18N
075: private static final String PP_DESCR = NbBundle.getMessage(
076: LoadGenProfilingPointFactory.class,
077: "LoadGenProfilingPointFactory_PpDescr"); // NOI18N
078: private static final String PP_DEFAULT_NAME = NbBundle.getMessage(
079: LoadGenProfilingPointFactory.class,
080: "LoadGenProfilingPointFactory_PpDefaultName"); // NOI18N
081: // -----
082: private static final Icon LOADGEN_PP_ICON = new ImageIcon(
083: Utilities
084: .loadImage("org/netbeans/modules/profiler/ppoints/ui/resources/loadgenProfilingPoint.png")); // NOI18N
085: private static final String LOADGEN_PP_TYPE = PP_TYPE;
086: private static final String LOADGEN_PP_DESCR = PP_DESCR;
087: private static final String START_LOCATION_PREFIX = "start_"; // NOI18N
088: private static final String END_LOCATION_PREFIX = "end_"; // NOI18N
089: private static LoadGenProfilingPointFactory defaultInstance = null;
090:
091: //~ Instance fields ----------------------------------------------------------------------------------------------------------
092:
093: private final Lookup.Result loadGenResult;
094: private final LookupListener lookupListener = new LookupListener() {
095: public void resultChanged(LookupEvent lookupEvent) {
096: available = ((Lookup.Result) lookupEvent.getSource())
097: .allInstances().size() > 0;
098: firePropertyChange(new PropertyChangeEvent(
099: LoadGenProfilingPointFactory.this ,
100: ProfilingPointFactory.AVAILABILITY_PROPERTY, false,
101: true));
102: }
103: };
104:
105: private boolean available = false;
106:
107: //~ Constructors -------------------------------------------------------------------------------------------------------------
108:
109: public LoadGenProfilingPointFactory() {
110: loadGenResult = Lookup.getDefault().lookupResult(
111: LoadGenPlugin.class);
112: loadGenResult.addLookupListener(WeakListeners.create(
113: LookupListener.class, lookupListener, loadGenResult));
114: available = loadGenResult.allInstances().size() > 0;
115: }
116:
117: //~ Methods ------------------------------------------------------------------------------------------------------------------
118:
119: public static synchronized LoadGenProfilingPointFactory getDefault() {
120: return Lookup.getDefault().lookup(
121: LoadGenProfilingPointFactory.class);
122:
123: // if (defaultInstance == null) defaultInstance = new LoadGenProfilingPointFactory();
124: // return defaultInstance;
125: }
126:
127: public boolean isAvailable() {
128: return available;
129: }
130:
131: public String getDescription() {
132: return LOADGEN_PP_DESCR;
133: }
134:
135: public Icon getIcon() {
136: return LOADGEN_PP_ICON;
137: }
138:
139: public int getScope() {
140: return SCOPE_CODE;
141: }
142:
143: public String getType() {
144: return LOADGEN_PP_TYPE;
145: }
146:
147: public ProfilingPoint create(Project project) {
148: if (project == null) {
149: project = Utils.getCurrentProject(); // project not defined, will be detected from most active Editor or Main Project will be used
150: }
151:
152: CodeProfilingPoint.Location[] selectionLocations = Utils
153: .getCurrentSelectionLocations();
154:
155: if (selectionLocations.length != 2) {
156: CodeProfilingPoint.Location location = Utils
157: .getCurrentLocation(CodeProfilingPoint.Location.OFFSET_START);
158:
159: if (location.equals(CodeProfilingPoint.Location.EMPTY)) {
160: String filename = ""; // NOI18N
161: String name = Utils.getUniqueName(getType(), "",
162: project); // NOI18N
163:
164: return new LoadGenProfilingPoint(name, location, null,
165: project);
166: } else {
167: String filename = FileUtil.toFileObject(
168: new File(location.getFile())).getName();
169: String name = Utils.getUniqueName(getType(),
170: MessageFormat.format(PP_DEFAULT_NAME,
171: new Object[] { "", filename,
172: location.getLine() }), project); // NOI18N
173:
174: return new LoadGenProfilingPoint(name, location, null,
175: project);
176: }
177: } else {
178: CodeProfilingPoint.Location startLocation = selectionLocations[0];
179: CodeProfilingPoint.Location endLocation = selectionLocations[1];
180: String filename = FileUtil.toFileObject(
181: new File(startLocation.getFile())).getName();
182: String name = Utils.getUniqueName(getType(), MessageFormat
183: .format(PP_DEFAULT_NAME, new Object[] { "",
184: filename, startLocation.getLine() }),
185: project); // NOI18N
186:
187: return new LoadGenProfilingPoint(name, startLocation,
188: endLocation, project);
189: }
190: }
191:
192: public boolean supportsCPU() {
193: return true;
194: }
195:
196: public boolean supportsMemory() {
197: return true;
198: }
199:
200: public boolean supportsMonitor() {
201: return false;
202: }
203:
204: protected Class getProfilingPointsClass() {
205: return LoadGenProfilingPoint.class;
206: }
207:
208: protected String getServerHandlerClassName() {
209: return "org.netbeans.lib.profiler.global.ProfilingPointServerHandler"; // NOI18N
210: }
211:
212: protected ValidityAwarePanel createCustomizer() {
213: return new LoadGeneratorCustomizer(getType(), getIcon());
214: }
215:
216: protected ProfilingPoint loadProfilingPoint(Project project,
217: Properties properties, int index) {
218: String name = properties.getProperty(index + "_"
219: + ProfilingPoint.PROPERTY_NAME, null); // NOI18N
220: String enabledStr = properties.getProperty(index + "_"
221: + ProfilingPoint.PROPERTY_ENABLED, null); // NOI18N
222: String scriptFile = properties.getProperty(index + "_"
223: + LoadGenProfilingPoint.PROPERTY_SCRIPTNAME, null); // NOI18N
224: CodeProfilingPoint.Location startLocation = CodeProfilingPoint.Location
225: .load(project, index, START_LOCATION_PREFIX, properties);
226: CodeProfilingPoint.Location endLocation = CodeProfilingPoint.Location
227: .load(project, index, END_LOCATION_PREFIX, properties);
228:
229: if ((name == null) || (enabledStr == null)
230: || (startLocation == null)) {
231: return null;
232: }
233:
234: LoadGenProfilingPoint profilingPoint = null;
235:
236: try {
237: profilingPoint = new LoadGenProfilingPoint(name,
238: startLocation, endLocation, project);
239: profilingPoint.setEnabled(Boolean.parseBoolean(enabledStr));
240: profilingPoint.setSriptFileName(scriptFile);
241: } catch (Exception e) {
242: ErrorManager.getDefault().log(ErrorManager.ERROR,
243: e.getMessage());
244: }
245:
246: return profilingPoint;
247: }
248:
249: protected void storeProfilingPoint(ProfilingPoint profilingPoint,
250: int index, Properties properties) {
251: LoadGenProfilingPoint loadgen = (LoadGenProfilingPoint) profilingPoint;
252: properties.put(index + "_" + ProfilingPoint.PROPERTY_NAME,
253: loadgen.getName()); // NOI18N
254: properties.put(index + "_" + ProfilingPoint.PROPERTY_ENABLED,
255: Boolean.toString(loadgen.isEnabled())); // NOI18N
256: properties.put(index + "_"
257: + LoadGenProfilingPoint.PROPERTY_SCRIPTNAME, loadgen
258: .getScriptFileName()); // NOI18N
259: loadgen.getStartLocation().store(loadgen.getProject(), index,
260: START_LOCATION_PREFIX, properties);
261:
262: if (loadgen.usesEndLocation()) {
263: loadgen.getEndLocation().store(loadgen.getProject(), index,
264: END_LOCATION_PREFIX, properties);
265: }
266: }
267: }
|