/* Utilities for User Actor Libraries Copyright (c) 2006-2014 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.actor.gui; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import java.net.URL; import ptolemy.kernel.CompositeEntity; import ptolemy.kernel.Entity; import ptolemy.kernel.attributes.URIAttribute; import ptolemy.kernel.util.ChangeRequest; import ptolemy.kernel.util.IllegalActionException; import ptolemy.kernel.util.InternalErrorException; import ptolemy.kernel.util.NameDuplicationException; import ptolemy.kernel.util.StringAttribute; import ptolemy.moml.LibraryBuilder; import ptolemy.moml.MoMLChangeRequest; import ptolemy.moml.MoMLParser; import ptolemy.util.StringUtilities; /////////////////////////////////////////////////////////////////// //// UserActorLibrary /** Access the User Actor Library. @author Christopher Brooks, based on work by Steve Neuendorffer, Edward A. Lee, Contributor: Chad Berkeley (Kepler) @version $Id: UserActorLibrary.java 70398 2014-10-22 23:44:32Z cxh $ @since Ptolemy II 5.2 @Pt.ProposedRating Red (neuendor) @Pt.AcceptedRating Red (neuendor) */ public class UserActorLibrary { /** * Open the user actor library as a new library in the actor * library for this application. * *
The name of the user actor library consists of the * values of {@link ptolemy.util.StringUtilities#preferencesDirectory()} * and {@link #USER_LIBRARY_NAME} and ".xml" concatenated. * *
An alternate class can be used to build the library if reading the
* MoML is not desired. The class must extend ptolemy.moml.LibraryBuilder
* and the _alternateLibraryBuilder property must be set with the 'value'
* set to the class that extends LibraryBuilder.
*
* @param configuration The configuration where we look for the
* actor library.
* @exception Exception If there is a problem opening the configuration,
* opening the MoML file, or opening the MoML file as a new library.
*/
public static void openUserLibrary(Configuration configuration)
throws Exception {
// FIXME: If the name is something like
// "vergilUserLibrary.xml" then when we save an actor in the
// library and then save the window that comes up the name of
// entity gets set to vergilUserLibrary instead of the value
// of USER_LIBRARY_NAME. This causes problems when we
// try to save another file. The name of the entity gets
// changed by the saveAs code.
String libraryName = null;
try {
libraryName = StringUtilities.preferencesDirectory()
+ USER_LIBRARY_NAME + ".xml";
} catch (Exception ex) {
System.out.println("Warning: Failed to get the preferences "
+ "directory (-sandbox always causes this): " + ex);
}
if (libraryName != null) {
File file = new File(libraryName);
if (!file.isFile() || !file.exists()) {
// File might exist under an old name.
// Try to read it.
String oldLibraryName = StringUtilities.preferencesDirectory()
+ "user library.xml";
File oldFile = new File(oldLibraryName);
if (oldFile.isFile() && oldFile.exists()) {
if (!oldFile.renameTo(file)) {
throw new IOException("Failed to rename \"" + oldFile
+ "\" to \"" + file + "\".");
}
}
}
if (!file.isFile() || !file.exists()) {
FileWriter writer = null;
try {
if (!file.createNewFile()) {
throw new Exception(file + "already exists?");
}
writer = new FileWriter(file);
writer.write(" An alternate class can be used to build the library if reading
* the MoML is not desired. The class must extend
* ptolemy.moml.LibraryBuilder and the _alternateLibraryBuilder
* property must be set with the 'value' set to the class that
* extends LibraryBuilder. A library of components is a .xml file that defines a MoML Class
* that extends ptolemy.moml.EntityLibrary, for example, the following
* file creates a library called "MyActors" that has an XYPlotter
* in the library: Note that one restriction is to see the new library, one must
* open a new Graph viewer (New -> Graph). This variable is not final so that users of this class
* may change it.
*/
public static/* final */String USER_LIBRARY_NAME = "UserLibrary";
///////////////////////////////////////////////////////////////////
//// private methods ////
/**
* instantiate a ComponentEntity and create the changeRequest to
* implement it in the model
*/
private static void _instantiateLibrary(final CompositeEntity library,
final ModelDirectory directory, Configuration configuration,
File file, final CompositeEntity libraryContainer,
final PtolemyEffigy finalLibraryEffigy) throws Exception {
ChangeRequest request = new ChangeRequest(configuration, file.toURI()
.toURL().toString()) {
@Override
protected void _execute() throws Exception {
// The library is a class!
library.setClassDefinition(true);
library.instantiate(libraryContainer, library.getName());
finalLibraryEffigy.setContainer(directory);
}
};
libraryContainer.requestChange(request);
request.waitForCompletion();
}
}
* <?xml version="1.0" standalone="no"?>
* <!DOCTYPE class PUBLIC "-//UC Berkeley//DTD MoML 1//EN"
* "http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd">
* <class name="MyActors" extends="ptolemy.moml.EntityLibrary">
* <configure>
* <group>
* <entity name="XY Plotter" class="ptolemy.actor.lib.gui.XYPlotter"/>
* </group>
* </configure>
* </class>
*
*
*