Thread: [Piper-net-devel] SF.net SVN: piper-net:[12] trunk/piper
Status: Pre-Alpha
Brought to you by:
rdodgen
From: <trh...@us...> - 2009-01-10 06:07:41
|
Revision: 12 http://piper-net.svn.sourceforge.net/piper-net/?rev=12&view=rev Author: trhodes1517 Date: 2009-01-10 06:07:30 +0000 (Sat, 10 Jan 2009) Log Message: ----------- Changed the structure of plugins and added an example plugin. Note that this is only the rough beginnings of the base plugin structure. Added Paths: ----------- trunk/piper/src/net/piper/plugins/Plugin.java trunk/piper/src/net/piper/plugins/PluginPanel.java trunk/piper/src/net/piper/plugins/PluginSettings.java trunk/piper/src/net/piper/plugins/PluginSettingsPanel.java trunk/piper/test/TestPluginStuff.java Removed Paths: ------------- trunk/piper/src/net/piper/plugins/MyFirstPlugin.java trunk/piper/src/net/piper/plugins/Plugin.java trunk/piper/src/net/piper/plugins/PluginSettings.java trunk/piper/src/net/piper/plugins/PluginSettingsPersistenceDelegate.java trunk/piper/test/TestXMLSettings.java Deleted: trunk/piper/src/net/piper/plugins/MyFirstPlugin.java =================================================================== --- trunk/piper/src/net/piper/plugins/MyFirstPlugin.java 2009-01-04 01:27:17 UTC (rev 11) +++ trunk/piper/src/net/piper/plugins/MyFirstPlugin.java 2009-01-10 06:07:30 UTC (rev 12) @@ -1,16 +0,0 @@ -package net.piper.plugins; - -public class MyFirstPlugin extends Plugin { - - public boolean performAction() { - PluginSettings settings = getSettings(); - - settings.addSetting( "key", "abc" ); - - return true; - } - - public String getName() { - return "MyFirstPlugin"; - } -} \ No newline at end of file Deleted: trunk/piper/src/net/piper/plugins/Plugin.java =================================================================== --- trunk/piper/src/net/piper/plugins/Plugin.java 2009-01-04 01:27:17 UTC (rev 11) +++ trunk/piper/src/net/piper/plugins/Plugin.java 2009-01-10 06:07:30 UTC (rev 12) @@ -1,120 +0,0 @@ -package net.piper.plugins; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileInputStream; -import java.io.FileOutputStream; - -import java.beans.XMLDecoder; -import java.beans.XMLEncoder; - -import java.util.Map; -public abstract class Plugin { - - public static final String PLUGIN_DIR = "plugins"; - - private PluginSettings settings; - private File xmlFile; - - /** - * Main way to execute the action of a plugin. - **/ - public abstract boolean performAction(); - - /** - * This is used to determine where to save the file. - * Also it is helpful when creating the GUI representing - * the various plugins. - **/ - public abstract String getName(); - - // public abstract void initializeSettingsDialog(); - - /** - * This method will be called after the 'settings' dialog for the - * current plugin is closed. - **/ - public void modifySettings( Map<String, String> newSettings ) { - if( settings == null ) return; - - for( String key : newSettings.keySet() ) - settings.addSetting( key, newSettings.get(key) ); - } - - /** - * Completely erases all settings from memory and disk. - **/ - public boolean eraseSettings() { - //returns true, because no settings have been added - if( xmlFile == null ) return true; - - boolean b = xmlFile.delete(); - settings = null; - - return b; - } - - /** - * Gets all settings. If the internal PluginSettings has not been set, - * getSettings looks for a settings file to read from. - **/ - public PluginSettings getSettings() { - - if( settings == null ) { - xmlFile = new File(PLUGIN_DIR + File.separator - + this.getName()+"Settings" + ".xml"); - - //makes sure that the parent path is valid. - xmlFile.getParentFile().mkdirs(); - - if( xmlFile.exists() ) { - try{ - FileInputStream in = new FileInputStream( xmlFile ); - XMLDecoder decode = new XMLDecoder( in ); - settings = (PluginSettings)decode.readObject(); - } catch( FileNotFoundException e ) { - System.err.println( "Cannot Find file: "+xmlFile.toString() - +"\nThis should never happen" ); - } - } else { - settings = new PluginSettings(); - } - } - - return settings; - } - - /** - * Writes all of the settings in the contained pluginSettings class - **/ - public void writeSettings() { - - if( settings == null || xmlFile == null ) - return; - - try{ - FileOutputStream out = new FileOutputStream( xmlFile ); - XMLEncoder encode = new XMLEncoder( out ); - - //Uses the custom made PersistenceDelegator - encode.setPersistenceDelegate( PluginSettings.class, - new PluginSettingsPersistenceDelegate() ); - - encode.writeObject( settings ); - encode.close(); - } catch( FileNotFoundException e ) { - System.err.println("Cannot write to file: "+xmlFile.toString() ); - } - } - - // public void displaySettingsDialog() {} - - /** - * Should be called when the plugin is no longer used. This saves the - * plugins current settings. - **/ - public void close() { - writeSettings(); - } - -} \ No newline at end of file Added: trunk/piper/src/net/piper/plugins/Plugin.java =================================================================== --- trunk/piper/src/net/piper/plugins/Plugin.java (rev 0) +++ trunk/piper/src/net/piper/plugins/Plugin.java 2009-01-10 06:07:30 UTC (rev 12) @@ -0,0 +1,91 @@ +package net.piper.plugins; + +import javax.swing.JPanel; +import javax.swing.JDialog; +import javax.swing.JButton; + +import java.awt.Frame; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.Dimension; + +public abstract class Plugin extends JPanel{ + + private final String DEFAULT_NAME = this.getClass().getSimpleName(); + + /** + * Need to do send/recieveAction. Check metadata. + **/ + + /** + * Main way to execute the action of a plugin. + **/ + public abstract boolean performAction(); + + public abstract PluginSettingsPanel getSettingsPanel(); + + public void initVisualComponents() { + + JButton go = new JButton("Go: "+DEFAULT_NAME) ; + go.addActionListener( new ActionListener() { + public void actionPerformed( ActionEvent e ) { + String message = "Action succesful!"; + if( !performAction() ) { + message = "Action Unsuccesful. Printing to log"; + + } + + } + }); + this.add( go ); + + JButton settings = new JButton("Settings") ; + settings.addActionListener( new ActionListener() { + public void actionPerformed( ActionEvent e ) { + JDialog settingsDialog = new JDialog( Frame.getFrames()[0], DEFAULT_NAME + " Settings", true ); + + JPanel myPanel = new JPanel(); + myPanel.setPreferredSize( new Dimension( 600, 400 ) ); + + settingsDialog.setContentPane(myPanel); + + settingsDialog.getContentPane().add( getSettingsPanel() ); + + JButton apply = new JButton("Apply Settings"); + apply.addActionListener( new ActionListener() { + public void actionPerformed( ActionEvent e ) { + getSettingsPanel().applySettings(); + // settingsDialog.dispose(); + } + }); + + settingsDialog.getContentPane().add( apply ); + + JButton save = new JButton("Save Settings"); + save.addActionListener( new ActionListener() { + public void actionPerformed( ActionEvent e ) { + getSettingsPanel().saveSettings(); + // settingsDialog.dispose(); + } + }); + + settingsDialog.getContentPane().add( save ); + + settingsDialog.setResizable( false ); + settingsDialog.pack(); + + settingsDialog.setVisible( true ); + } + }); + this.add( settings ); + } + + /** + * Should be called when the plugin is no longer used. This saves the + * plugins current settings. + **/ + public void close() { + getSettingsPanel().writeSettings(); + } + +} \ No newline at end of file Property changes on: trunk/piper/src/net/piper/plugins/Plugin.java ___________________________________________________________________ Added: svn:executable + * Added: trunk/piper/src/net/piper/plugins/PluginPanel.java =================================================================== --- trunk/piper/src/net/piper/plugins/PluginPanel.java (rev 0) +++ trunk/piper/src/net/piper/plugins/PluginPanel.java 2009-01-10 06:07:30 UTC (rev 12) @@ -0,0 +1,16 @@ +package net.piper.plugins; + +import javax.swing.JPanel; +import javax.swing.JButton; + + +public class PluginPanel extends JPanel{ + + public PluginPanel() { + initComponents(); + } + + public void initComponents() { + + } +} \ No newline at end of file Property changes on: trunk/piper/src/net/piper/plugins/PluginPanel.java ___________________________________________________________________ Added: svn:executable + * Deleted: trunk/piper/src/net/piper/plugins/PluginSettings.java =================================================================== --- trunk/piper/src/net/piper/plugins/PluginSettings.java 2009-01-04 01:27:17 UTC (rev 11) +++ trunk/piper/src/net/piper/plugins/PluginSettings.java 2009-01-10 06:07:30 UTC (rev 12) @@ -1,30 +0,0 @@ -package net.piper.plugins; - -import java.util.TreeMap; -import java.util.Collection; - -public class PluginSettings { - - private TreeMap<String, String> settings; - - public PluginSettings() { - settings = new TreeMap<String, String> (); - } - - public void addSetting( String key, String value ) { - settings.put( key, value ); - } - - public String removeSetting( String key ) { - return settings.remove( key ); - } - - public Collection<String> getAllKeys() { - return settings.keySet(); - } - - public String getSetting( String key ) { - return settings.get( key ); - } - -} \ No newline at end of file Added: trunk/piper/src/net/piper/plugins/PluginSettings.java =================================================================== --- trunk/piper/src/net/piper/plugins/PluginSettings.java (rev 0) +++ trunk/piper/src/net/piper/plugins/PluginSettings.java 2009-01-10 06:07:30 UTC (rev 12) @@ -0,0 +1,5 @@ +package net.piper.plugins; + +public abstract class PluginSettings { + +} \ No newline at end of file Property changes on: trunk/piper/src/net/piper/plugins/PluginSettings.java ___________________________________________________________________ Added: svn:executable + * Added: trunk/piper/src/net/piper/plugins/PluginSettingsPanel.java =================================================================== --- trunk/piper/src/net/piper/plugins/PluginSettingsPanel.java (rev 0) +++ trunk/piper/src/net/piper/plugins/PluginSettingsPanel.java 2009-01-10 06:07:30 UTC (rev 12) @@ -0,0 +1,83 @@ +package net.piper.plugins; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileInputStream; +import java.io.FileOutputStream; + +import java.beans.XMLDecoder; +import java.beans.XMLEncoder; + +import javax.swing.JPanel; + +public abstract class PluginSettingsPanel extends JPanel{ + public abstract void saveSettings(); + public abstract void applySettings(); + + public abstract PluginSettings getSettings(); + + private final String DEFAULT_NAME = this.getClass().getSimpleName(); + private File xmlFile; + + public static final String PLUGIN_DIR = "plugins"; + + + /** + * Gets all settings. If the internal PluginSettings has not been set, + * getSettings looks for a settings file to read from. + **/ + public PluginSettings getSettingsFromFile() { + + if( xmlFile == null ) + setXMLFile( DEFAULT_NAME ); + + PluginSettings settings = null; + + if( xmlFile.exists() ) { + try{ + FileInputStream in = new FileInputStream( xmlFile ); + XMLDecoder decode = new XMLDecoder( in ); + settings = (PluginSettings)decode.readObject(); + } catch( FileNotFoundException e ) { + System.err.println( "Cannot Find file: "+xmlFile.toString() + +"\nThis should never happen" ); + } + } + + return settings; + } + + /** + * Writes all of the settings in the contained pluginSettings class + **/ + public void writeSettings() { + + if( xmlFile == null ) + setXMLFile( DEFAULT_NAME ); + + + if( xmlFile.exists() ) + xmlFile.delete(); + else + xmlFile.getParentFile().mkdirs(); + + PluginSettings mySettings = getSettings(); + if( mySettings == null ) + return; + + try{ + FileOutputStream out = new FileOutputStream( xmlFile ); + XMLEncoder encode = new XMLEncoder( out ); + + encode.writeObject( mySettings ); + encode.close(); + } catch( FileNotFoundException e ) { + System.err.println("Cannot write to file: "+xmlFile.toString() ); + } + } + private void setXMLFile(String name) { + xmlFile = new File(PLUGIN_DIR + File.separator + + name + ".xml"); + } + +} \ No newline at end of file Property changes on: trunk/piper/src/net/piper/plugins/PluginSettingsPanel.java ___________________________________________________________________ Added: svn:executable + * Deleted: trunk/piper/src/net/piper/plugins/PluginSettingsPersistenceDelegate.java =================================================================== --- trunk/piper/src/net/piper/plugins/PluginSettingsPersistenceDelegate.java 2009-01-04 01:27:17 UTC (rev 11) +++ trunk/piper/src/net/piper/plugins/PluginSettingsPersistenceDelegate.java 2009-01-10 06:07:30 UTC (rev 12) @@ -1,29 +0,0 @@ -package net.piper.plugins; - -import java.beans.DefaultPersistenceDelegate; -import java.beans.Statement; -import java.beans.Encoder; - -import java.util.Map; -import java.util.TreeMap; - -public class PluginSettingsPersistenceDelegate - extends DefaultPersistenceDelegate { - - protected void initialize(Class type, Object oldInstance, - Object newInstance, Encoder out) { - // Note, the "size" property will be set here. - super.initialize(type, oldInstance, newInstance, out); - - PluginSettings m = - (PluginSettings)oldInstance; - for ( String s : m.getAllKeys() ) { - out.writeStatement( - new Statement(oldInstance, - "addSetting", // Could also use "addElement" here. - new String[]{ s, m.getSetting(s)}) ); - } - } - - -} \ No newline at end of file Added: trunk/piper/test/TestPluginStuff.java =================================================================== --- trunk/piper/test/TestPluginStuff.java (rev 0) +++ trunk/piper/test/TestPluginStuff.java 2009-01-10 06:07:30 UTC (rev 12) @@ -0,0 +1,23 @@ +package test; + +import javax.swing.JFrame; + +import net.piper.plugins.*; +import net.piper.plugins.Shutdown.*; + +public class TestPluginStuff extends JFrame{ + + public static void main(String[] args ) { + TestPluginStuff test = new TestPluginStuff(); + } + + public TestPluginStuff () { + super("MyPluginTest"); + ShutdownPlugin sp = new ShutdownPlugin(); + + this.add(sp); + this.pack(); + this.setVisible(true); + } + +} \ No newline at end of file Deleted: trunk/piper/test/TestXMLSettings.java =================================================================== --- trunk/piper/test/TestXMLSettings.java 2009-01-04 01:27:17 UTC (rev 11) +++ trunk/piper/test/TestXMLSettings.java 2009-01-10 06:07:30 UTC (rev 12) @@ -1,75 +0,0 @@ -package net.piper.plugins.test; - -import junit.framework.*; - -import net.piper.plugins.*; - -import java.util.TreeMap; -import java.util.Map; - -import java.beans.XMLEncoder; -import java.beans.XMLDecoder; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.FileInputStream; - -public class TestXMLSettings extends TestCase { - - public void testSimplePlugin() { - MyFirstPlugin test = new MyFirstPlugin(); - - test.performAction(); - - test.close(); - } - - public void testReadAndWrite( ) throws Exception { - PluginSettings test = new PluginSettings(); - PluginSettings readTest; - TreeMap<String, String> data = generateSettings(50); - for( String s : data.keySet() ) - test.addSetting( s, data.get( s ) ); - - File f = new File( "testSettings.xml" ); - FileOutputStream out = new FileOutputStream( f ); - - XMLEncoder xmlEncode = new XMLEncoder( out ); - xmlEncode.setPersistenceDelegate(PluginSettings.class, - new PluginSettingsPersistenceDelegate() ); - - xmlEncode.writeObject( test ); - xmlEncode.close(); - out.close(); - - FileInputStream in = new FileInputStream( f ); - - XMLDecoder xmlDecode = new XMLDecoder( in ); - readTest = (PluginSettings)xmlDecode.readObject(); - - for( String s : data.keySet() ) - assertEquals( data.get( s ), readTest.getSetting( s ) ); - } - public static TreeMap<String, String> generateSettings(int count) { - - TreeMap<String, String> out = new TreeMap<String, String>(); - for( int i = 0; i < count; i++ ) { - String key = randomString(); - String value = randomString(); - out.put( key, value ); - } - - return out; - } - public static String randomString() { - - int len = (int) (Math.random()*20)+1; - String out = ""; - - for( int i = 0; i < len; i ++ ) - out += (char) ((int) (Math.random()*26)+ (int)'A'); - - return out; - } - -} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |