diff --git a/.classpath b/.classpath deleted file mode 100644 index 01bbadf..0000000 --- a/.classpath +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d177073 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +/.idea +lcdjava.iml + +/target diff --git a/.project b/.project deleted file mode 100644 index 7b3cfb9..0000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - lcdjava - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e405d2a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +# Change Log +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +## [Unreleased] + +### Changed + +- Big rename! Project is now packaged under *org.lcdproc.lcdjava*. + Before it was *org.boncey.lcdjava*. + +## 0.5.0 - 2016-08-31 +### Added +- First packaged release after over 10 years from the start of the + project. There have been no major API changes in this version + compared to the old unreleased code, but there is probably some minor + thing that is different. It does however require a modern JDK (1.8) + and Maven has replaced Ant as the build system. + +[Unreleased]: https://github.com/lcdproc/lcdjava/compare/0.5.0...HEAD diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..ca278e4 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,13 @@ +Contributing to LCDjava +======================= + +Github standard procedure. Submit issues if you find bugs. Clone, edit and create PR to submit patches. + +Branches +======== + +The tip of *master* is kept at high quality. Changes may be committed directly to *master* but there are no force-pushes to this branch. + +Sometimes half-finished work is committed to *dev*. This branch is unstable, may be rewritten at any time and might not even build. Eventually *dev* is taken to production quality and is merged into *master*. + +Feature branches may be considered at a later stage if/when there is more than one contributor. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e302a0e --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2017 LCDjava contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README deleted file mode 100644 index c4b2cae..0000000 --- a/README +++ /dev/null @@ -1,11 +0,0 @@ -Java client for LCDproc (http://lcdproc.org/). - -Code status: untested and possibly out of date. - -I used to use this with LCDproc on Linux a few years back but I have stopped using it now. -I don't even have an LCD display hooked up anymore so am unable to test it. - - -However, when I did use it seemed to work fine - I ran it as part of my Wiphi home music player project (http://boncey.org/tags/wiphi). - -I'm happy to answer questions about the code via http://github.com/boncey/lcdjava. diff --git a/README b/README new file mode 120000 index 0000000..42061c0 --- /dev/null +++ b/README @@ -0,0 +1 @@ +README.md \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..27311f9 --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +LCDjava +======= + +Java client for LCDproc (http://lcdproc.org/). + +This project was originally created by Darren Greaves in 2004-2008. It has been +updated in 2016 and builds with JDK 1.8 and Maven 3.3. + +Building +-------- + +To build the project: + + $ mvn compile + +To generate documentation: + + $ mvn javadoc:javadoc + +You can also run `mvn site` to generate a full Maven website, including the Javadocs. + +To package the project (excluding dependencies): + + $ mvn package + +Example usage +------------- + +See [lcdjava-demos][] for two example projects using LCDjava. They demonstrate both how to use the LCDjava API and how to integrate the library in a larger Maven application. + +[lcdjava-demos]: https://github.com/lcdproc/lcdjava-demos + +Project information +------------------- + +I used to use this with LCDproc on Linux a few years back but I have stopped +using it now. I don't even have an LCD display hooked up anymore so am unable to +test it. + +However, when I did use it seemed to work fine - I ran it as part of my Wiphi +home music player project (http://boncey.org/tags/wiphi). + +I'm happy to answer questions about the code via +http://github.com/lcdproc/lcdjava. + +Project dependencies +-------------------- + +LCDjava uses the [Simple Logging Facade for Java (SLF4J)][slf4j] as its logger abstracter so as to not dictate a logging implementation on its users. SLF4J supports logging to its built-in Simple Logger (logs everything to the console), Log4j 1.2 and 2.x, Logback and java.util.logging. + +[slf4j]: http://www.slf4j.org/ diff --git a/build.xml b/build.xml deleted file mode 100644 index 6c8799a..0000000 --- a/build.xml +++ /dev/null @@ -1,107 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/build/lib/jdom.jar b/build/lib/jdom.jar deleted file mode 100644 index 288e64c..0000000 Binary files a/build/lib/jdom.jar and /dev/null differ diff --git a/build/lib/log4j-1.2.6.jar b/build/lib/log4j-1.2.6.jar deleted file mode 100644 index be4a917..0000000 Binary files a/build/lib/log4j-1.2.6.jar and /dev/null differ diff --git a/build/lib/rome-0.4.jar b/build/lib/rome-0.4.jar deleted file mode 100644 index a370dff..0000000 Binary files a/build/lib/rome-0.4.jar and /dev/null differ diff --git a/build/lib/rome-fetcher-0.4.jar b/build/lib/rome-fetcher-0.4.jar deleted file mode 100644 index c771e4f..0000000 Binary files a/build/lib/rome-fetcher-0.4.jar and /dev/null differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..b987377 --- /dev/null +++ b/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + + org.lcdproc.lcdjava + lcdjava + 0.6.0-SNAPSHOT + jar + + lcdjava + https://github.com/lcdproc/lcdjava + + + UTF-8 + + + + + + maven-compiler-plugin + 2.3.2 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + + + + + org.slf4j + slf4j-api + 1.7.21 + + + + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.9 + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.10.3 + + + + diff --git a/src/main/java/org/lcdproc/lcdjava/AbstractMenuItem.java b/src/main/java/org/lcdproc/lcdjava/AbstractMenuItem.java new file mode 100644 index 0000000..4ca54ca --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/AbstractMenuItem.java @@ -0,0 +1,164 @@ +package org.lcdproc.lcdjava; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.event.EventListenerList; + + +/** + * Abstract parent class for menu items. + * @author Stefan Krupop + */ +public abstract class AbstractMenuItem implements MenuItem +{ + /** + * The item id. + */ + protected final String _id; + + /** + * The item text. + */ + private String _text; + + /** + * The menu this item belongs to. + */ + protected Submenu _menu; + + /** + * The event listeners connected to this item + */ + protected EventListenerList _listeners; + + /** + * Constructor. + * @param id the of the item. + * @param menu the menu to add this item to + */ + protected AbstractMenuItem(String id, Submenu menu) + { + _id = id; + _menu = menu; + _listeners = new EventListenerList(); + } + + /** + * Get the item id. + * @return the item id. + */ + public String getID() + { + return _id; + } + + /** + * Set the menu item text. + * @param text the menu item text. + */ + public void setText(String text) + { + _text = text; + update(); + } + + /** + * Get the menu item text. + * @return the menu item text. + */ + public String getText() + { + return _text; + } + + /** + * Add this MenuItem to the Submenu that constructed us. + * @return whether or not the menu item was added successfully. + */ + public boolean activate() + { + return _menu.addItem(this); + } + + /** + * Remove this MenuItem from the Screen that constructed us. + */ + public void remove() + { + _menu.removeItem(this); + } + + /** + * Strip any quotes from the provided text. + * @param text the text to strip quotes from. + * @return the text with any quotes removed. + */ + protected String stripQuotes(String text) + { + StringBuffer ret = new StringBuffer(); + if (text != null) + { + char[] chars = text.toCharArray(); + for (int i = 0; i < chars.length; i++) + { + if (chars[i] != '\"') + { + ret.append(chars[i]); + } + } + } + return (text == null) ? null : ret.toString(); + } + + /** + * Update this MenuItems's state. + */ + protected void update() + { + _menu.updateItem(this); + } + + /** + * Return the data this menu item needs to update itself. + * @return the data to update this menu item. + */ + public String getData() + { + return "-text \"" + stripQuotes(_text) + "\""; + } + + /** + * Adds an ActionListener to the menu item. + * @param listener the ActionListener to be added + */ + public void addActionListener(ActionListener listener) { + _listeners.add(ActionListener.class, listener); + } + + /** + * Removes an ActionListener from the menu item. + * @param listener the ActionListener to be removed + */ + public void removeActionListener(ActionListener listener) { + _listeners.remove(ActionListener.class, listener); + } + + public void notifyActionPerformed(ActionEvent e) { + for (ActionListener l : _listeners.getListeners(ActionListener.class)) { + l.actionPerformed(e); + } + } + + /** + * Return a String representing this MenuItem. + * @return a String representing this MenuItem. + */ + public String toString() + { + return "Type = " + getType() + + "; menu id = " + _menu.getID() + + "; id = " + getID() + + "; data = " + getData(); + } +} diff --git a/src/org/boncey/lcdjava/AbstractWidget.java b/src/main/java/org/lcdproc/lcdjava/AbstractWidget.java similarity index 91% rename from src/org/boncey/lcdjava/AbstractWidget.java rename to src/main/java/org/lcdproc/lcdjava/AbstractWidget.java index 007e2c2..55ea70b 100644 --- a/src/org/boncey/lcdjava/AbstractWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/AbstractWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Abstract parent class for Widgets. @@ -17,16 +15,11 @@ public abstract class AbstractWidget implements Widget "$Id: AbstractWidget.java,v 1.3 2008-07-06 15:38:34 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - - /** * The Widget id. */ private int _id; - /** + /** * The Screen this Widget belongs to. */ private Screen _screen; @@ -42,7 +35,7 @@ protected AbstractWidget(int id, Screen screen) _screen = screen; } - /** + /** * Get the Widget id. * @return the Widget id. */ @@ -51,7 +44,7 @@ public int getId() return _id; } - /** + /** * Add this Widget to the Screen that constructed us. * @return whether or not the widget was added successfully. */ @@ -60,7 +53,7 @@ public boolean activate() return _screen.addWidget(this); } - /** + /** * Remove this Widget from the Screen that constructed us. */ public void remove() @@ -68,7 +61,7 @@ public void remove() _screen.removeWidget(this); } - /** + /** * Strip any quotes from the provided text. * @param text the text to strip quotes from. * @return the text with any quotes removed. @@ -90,7 +83,7 @@ protected String stripQuotes(String text) return (text == null) ? null : ret.toString(); } - /** + /** * Update this Widget's state. */ protected void update() diff --git a/src/main/java/org/lcdproc/lcdjava/ActionMenuItem.java b/src/main/java/org/lcdproc/lcdjava/ActionMenuItem.java new file mode 100644 index 0000000..4546d59 --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/ActionMenuItem.java @@ -0,0 +1,130 @@ +package org.lcdproc.lcdjava; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + + + +/** + * ActionMenuItem. + *

Item to trigger an action + * @author StefanKrupop + */ +public class ActionMenuItem extends AbstractMenuItem +{ + public enum MenuResult { + /** + * Menu stays as it is + */ + None("none"), + /** + * Menu closes and returns to a higher level + */ + Close("close"), + /** + * Quits the menu completely + */ + Quit("quit"); + + public String val; + + MenuResult(String val) { + this.val = val; + } + } + + private MenuResult _menuResult; + + /** + * Constructor. + * @param id the of the action. + * @param menu the Submenu that knows about this MenuItem. + */ + protected ActionMenuItem(String id, Submenu menu) + { + super(id, menu); + _menuResult = MenuResult.None; + } + + /** + * Get the menu item type. + * @return the menu item type. + */ + public String getType() + { + return MENUITEM_ACTION; + } + + /** + * Sets what to do with the menu when this action is selected + * @param result the MenuResult + */ + public void setMenuResult(MenuResult result) { + _menuResult = result; + update(); + } + + @Override + public String getData() { + return super.getData() + " -menu_result " + _menuResult.val; + } + + /** + * Construct a new ActionMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @return a new ActionMenuItem. + */ + public static ActionMenuItem construct(Submenu menu, String text) + { + return construct(menu, text, MenuResult.None); + } + + /** + * Construct a new ActionMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @param result the MenuResult defining what to do when action is selected + * @return a new ActionMenuItem. + */ + public static ActionMenuItem construct(Submenu menu, String text, MenuResult result) + { + ActionMenuItem menuItem = null; + + try + { + menuItem = (ActionMenuItem)menu.constructMenuItem(MENUITEM_ACTION); + menuItem.setMenuResult(result); + menuItem.setText(text); + } + catch (LCDException e) //NOPMD + { + // Supress, would only get one if we asked for an invalid menu item. + } + + return menuItem; + } + + /** + * Adds an ActionListener to the menu item. + * @param listener the ActionListener to be added + */ + public void addActionListener(ActionListener listener) { + _listeners.add(ActionListener.class, listener); + } + + /** + * Removes an ActionListener from the menu item. + * @param listener the ActionListener to be removed + */ + public void removeActionListener(ActionListener listener) { + _listeners.remove(ActionListener.class, listener); + } + + public void notifyActionPerformed(ActionEvent e) { + for (ActionListener l : _listeners.getListeners(ActionListener.class)) { + l.actionPerformed(e); + } + } +} + diff --git a/src/main/java/org/lcdproc/lcdjava/AlphaMenuItem.java b/src/main/java/org/lcdproc/lcdjava/AlphaMenuItem.java new file mode 100644 index 0000000..c133525 --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/AlphaMenuItem.java @@ -0,0 +1,194 @@ +package org.lcdproc.lcdjava; + + + + +/** + * AlphaMenuItem. + *

Is visible as a text. When selected, a screen comes up that shows the + * current string value, that you can edit with the cursor keys and Enter. + * The string is ended by selecting a 'null' input character. After that the + * menu returns. + * @author StefanKrupop + */ +public class AlphaMenuItem extends AbstractMenuItem +{ + private String _value; + private int _minlength; + private int _maxlength; + private String _passwordchar; + private boolean _allowCaps; + private boolean _allowNonCaps; + private boolean _allowNumbers; + private String _allowedExtra; + + /** + * Constructor. + * @param id the of the action. + * @param menu the Submenu that knows about this MenuItem. + */ + protected AlphaMenuItem(String id, Submenu menu) + { + super(id, menu); + _value = ""; + _minlength = 0; + _maxlength = 10; + _passwordchar = ""; + _allowCaps = true; + _allowNonCaps = true; + _allowNumbers = true; + _allowedExtra = ""; + } + + /** + * Get the menu item type. + * @return the menu item type. + */ + public String getType() + { + return MENUITEM_ALPHA; + } + + /** + * Sets current value of the entry + * @param value current value + */ + public void setValue(String value) { + _value = value; + update(); + } + + /** + * Sets current value of the entry but does not send the change to LCDd + * @param value current value + */ + public void setValueNoUpdate(String value) { + _value = value; + } + + /** + * Gets the currently set value + * @return value current value + */ + public String getValue() { + return _value; + } + + /** + * Sets the minimum length of the string that can be entered + * @param value min length + */ + public void setMinLength(int value) { + _minlength = value; + update(); + } + + /** + * Sets the maximum length of the string that can be entered + * @param value max length + */ + public void setMaxLength(int value) { + _maxlength = value; + update(); + } + + /** + * Sets the character that should be displayed instead of typed characters + * @param value password character + */ + public void setPasswordChar(String value) { + _passwordchar = value; + update(); + } + + /** + * Sets if upper case characters can be entered + * @param value new state + */ + public void setAllowCaps(boolean value) { + _allowCaps = value; + update(); + } + + /** + * Sets if lower case characters can be entered + * @param value new state + */ + public void setAllowNonCaps(boolean value) { + _allowNonCaps = value; + update(); + } + + /** + * Sets if numbers can be entered + * @param value new state + */ + public void setAllowNumbers(boolean value) { + _allowNumbers = value; + update(); + } + + /** + * Sets additional characters that can be entered + * @param chars allowed characters + */ + public void setAllowExtra(String chars) { + _allowedExtra = chars; + update(); + } + + @Override + public String getData() { + StringBuilder strb = new StringBuilder(); + strb.append(super.getData()); + strb.append(" -value \""); + strb.append(_value); + strb.append("\" -minlength "); + strb.append(_minlength); + strb.append(" -maxlength "); + strb.append(_maxlength); + if (!_passwordchar.isEmpty()) { + strb.append(" -password_char \""); + strb.append(_passwordchar); + strb.append('"'); + } + strb.append(" -allow_caps "); + strb.append(_allowCaps ? "true" : "false"); + strb.append(" -allow_noncaps "); + strb.append(_allowNonCaps ? "true" : "false"); + strb.append(" -allow_numbers "); + strb.append(_allowNumbers ? "true" : "false"); + if (!_allowedExtra.isEmpty()) { + strb.append(" -allowed_extra \""); + strb.append(_allowedExtra); + strb.append('"'); + } + + return strb.toString(); + } + + /** + * Construct a new AlphaMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @param current the current string + * @return a new AlphaMenuItem. + */ + public static AlphaMenuItem construct(Submenu menu, String text, String current) + { + AlphaMenuItem menuItem = null; + + try + { + menuItem = (AlphaMenuItem)menu.constructMenuItem(MENUITEM_ALPHA); + menuItem.setValue(current); + menuItem.setText(text); + } + catch (LCDException e) //NOPMD + { + // Supress, would only get one if we asked for an invalid menu item. + } + + return menuItem; + } +} diff --git a/src/org/boncey/lcdjava/BarWidget.java b/src/main/java/org/lcdproc/lcdjava/BarWidget.java similarity index 82% rename from src/org/boncey/lcdjava/BarWidget.java rename to src/main/java/org/lcdproc/lcdjava/BarWidget.java index c54135e..4a45d7b 100644 --- a/src/org/boncey/lcdjava/BarWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/BarWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Abstract Widget for displaying Bars. @@ -8,7 +6,7 @@ * @author Darren Greaves * @version $Id: BarWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public abstract class BarWidget extends PositionalWidget +public abstract class BarWidget extends PositionalWidget { /** * Version details. @@ -17,11 +15,6 @@ public abstract class BarWidget extends PositionalWidget "$Id: BarWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - - /** * The length of the Widget. */ private int _length; @@ -46,7 +39,7 @@ public void setLength(int length) update(); } - /** + /** * Get the Widget length. * @return the Widget length. */ @@ -55,7 +48,7 @@ public int getLength() return _length; } - /** + /** * Return the data this Widget needs to update itself. * @return the data to update this Widget. */ diff --git a/src/main/java/org/lcdproc/lcdjava/CheckboxMenuItem.java b/src/main/java/org/lcdproc/lcdjava/CheckboxMenuItem.java new file mode 100644 index 0000000..2330571 --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/CheckboxMenuItem.java @@ -0,0 +1,141 @@ +package org.lcdproc.lcdjava; + + + + +/** + * CheckboxnMenuItem. + *

Item to display a slectable status indicator + * @author StefanKrupop + */ +public class CheckboxMenuItem extends AbstractMenuItem +{ + public enum CheckboxValue { + /** + * Off + */ + Off("off"), + /** + * On + */ + On("on"), + /** + * Grayed out + */ + Gray("gray"); + + public String val; + + CheckboxValue(String val) { + this.val = val; + } + } + + private CheckboxValue _value; + private boolean _allowGray; + + /** + * Constructor. + * @param id the of the action. + * @param menu the Submenu that knows about this MenuItem. + */ + protected CheckboxMenuItem(String id, Submenu menu) + { + super(id, menu); + _allowGray = false; + _value = CheckboxValue.Off; + } + + /** + * Get the menu item type. + * @return the menu item type. + */ + public String getType() + { + return MENUITEM_CHECKBOX; + } + + /** + * Sets if a grayed checkbox is allowed + * @param allowGray whether grayed checkbox is allowed + */ + public void setAllowGray(boolean allowGray) { + _allowGray = allowGray; + update(); + } + + /** + * Sets current value of a checkbox + * @param value current status + */ + public void setValue(CheckboxValue value) { + _value = value; + update(); + } + + /** + * Sets current value of a checkbox but does not send the change to LCDd + * @param value current status + */ + public void setValueNoUpdate(CheckboxValue value) { + _value = value; + } + + /** + * Gets the current status of the checkbox + * @return status of the checkbox + */ + public CheckboxValue getValue() { + return _value; + } + + /** + * Gets the current status of the checkbox + * @return whether the checkbox is On + */ + public boolean isSelected() { + return _value.equals(CheckboxValue.On); + } + + @Override + public String getData() { + return super.getData() + " -value " + _value.val + " -allow_gray " + (_allowGray ? "true" : "false"); + } + + /** + * Construct a new CheckboxMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @return a new ActionMenuItem. + */ + public static CheckboxMenuItem construct(Submenu menu, String text) + { + return construct(menu, text, false); + } + + /** + * Construct a new ActionMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @param allowGray whether the "Gray" state is allowed + * @return a new ActionMenuItem. + */ + public static CheckboxMenuItem construct(Submenu menu, String text, boolean allowGray) + { + CheckboxMenuItem menuItem = null; + + try + { + menuItem = (CheckboxMenuItem)menu.constructMenuItem(MENUITEM_CHECKBOX); + menuItem.setAllowGray(allowGray); + menuItem.setText(text); + } + catch (LCDException e) //NOPMD + { + // Supress, would only get one if we asked for an invalid menu item. + } + + return menuItem; + } +} + diff --git a/src/org/boncey/lcdjava/HBarWidget.java b/src/main/java/org/lcdproc/lcdjava/HBarWidget.java similarity index 87% rename from src/org/boncey/lcdjava/HBarWidget.java rename to src/main/java/org/lcdproc/lcdjava/HBarWidget.java index bb9a50d..79c7057 100644 --- a/src/org/boncey/lcdjava/HBarWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/HBarWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Horizontal Bar Widget. @@ -9,7 +7,7 @@ * @author Darren Greaves * @version $Id: HBarWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public class HBarWidget extends BarWidget +public class HBarWidget extends BarWidget { /** * Version details. @@ -17,11 +15,6 @@ public class HBarWidget extends BarWidget public static final String CVSID = "$Id: HBarWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; - /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - /** * Constructor. * @param id the of the Widget. @@ -32,7 +25,7 @@ protected HBarWidget(int id, Screen screen) super(id, screen); } - /** + /** * Get the Widget type. * @return the Widget type. */ @@ -41,7 +34,7 @@ public String getType() return Widget.WIDGET_HBAR; } - /** + /** * Construct a new HBarWidget. * @param screen the Screen that owns the Widget. * @param x the x position. diff --git a/src/org/boncey/lcdjava/IconWidget.java b/src/main/java/org/lcdproc/lcdjava/IconWidget.java similarity index 88% rename from src/org/boncey/lcdjava/IconWidget.java rename to src/main/java/org/lcdproc/lcdjava/IconWidget.java index 9c7151d..8ccdfe6 100644 --- a/src/org/boncey/lcdjava/IconWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/IconWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Icon Widget. @@ -9,7 +7,7 @@ * @author Darren Greaves * @version $Id: IconWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public class IconWidget extends PositionalWidget +public class IconWidget extends PositionalWidget { /** * Version details. @@ -17,118 +15,113 @@ public class IconWidget extends PositionalWidget public static final String CVSID = "$Id: IconWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; - /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - - /** + /** * A predefined icon. */ public static final String ICON_BLOCK_FILLED = "BLOCK_FILLED"; - /** + /** * A predefined icon. */ public static final String ICON_HEART_OPEN = "HEART_OPEN"; - /** + /** * A predefined icon. */ public static final String ICON_HEART_FILLED = "HEART_FILLED"; - /** + /** * A predefined icon. */ public static final String ICON_ARROW_UP = "ARROW_UP"; - /** + /** * A predefined icon. */ public static final String ICON_ARROW_DOWN = "ARROW_DOWN"; - /** + /** * A predefined icon. */ public static final String ICON_ARROW_LEFT = "ARROW_LEFT"; - /** + /** * A predefined icon. */ public static final String ICON_ARROW_RIGHT = "ARROW_RIGHT"; - /** + /** * A predefined icon. */ public static final String ICON_CHECKBOX_OFF = "CHECKBOX_OFF"; - /** + /** * A predefined icon. */ public static final String ICON_CHECKBOX_ON = "CHECKBOX_ON"; - /** + /** * A predefined icon. */ public static final String ICON_CHECKBOX_GRAY = "CHECKBOX_GRAY"; - /** + /** * A predefined icon. */ public static final String ICON_SELECTOR_AT_LEFT = "SELECTOR_AT_LEFT"; - /** + /** * A predefined icon. */ public static final String ICON_SELECTOR_AT_RIGHT = "SELECTOR_AT_RIGHT"; - /** + /** * A predefined icon. */ public static final String ICON_ELLIPSIS = "ELLIPSIS"; - /** + /** * A predefined icon. */ public static final String ICON_PAUSE = "PAUSE"; - /** + /** * A predefined icon. */ public static final String ICON_PLAY = "PLAY"; - /** + /** * A predefined icon. */ public static final String ICON_PLAYR = "PLAYR"; - /** + /** * A predefined icon. */ public static final String ICON_FF = "FF"; - /** + /** * A predefined icon. */ public static final String ICON_FR = "FR"; - /** + /** * A predefined icon. */ public static final String ICON_NEXT = "NEXT"; - /** + /** * A predefined icon. */ public static final String ICON_PREV = "PREV"; - /** + /** * A predefined icon. */ public static final String ICON_REC = "REC"; - /** + /** * The Widget iconName. */ private String _iconName; @@ -143,13 +136,13 @@ protected IconWidget(int id, Screen screen) super(id, screen); } - /** + /** * Get the Widget type. * @return the Widget type. */ public String getType() { - return Widget.WIDGET_ICON; + return WIDGET_ICON; } /** @@ -162,7 +155,7 @@ public void setText(String iconName) update(); } - /** + /** * Get the Widget iconName. * @return the Widget iconName. */ @@ -171,7 +164,7 @@ public String getText() return _iconName; } - /** + /** * Return the data this Widget needs to update itself. * @return the data to update this Widget. */ @@ -180,7 +173,7 @@ public String getData() return super.getData() + "\"" + stripQuotes(_iconName) + "\""; } - /** + /** * Construct a new IconWidget. * @param screen the Screen that owns the Widget. * @param x the x position. @@ -197,7 +190,7 @@ public static IconWidget construct(Screen screen, try { - widget = (IconWidget)screen.constructWidget(Widget.WIDGET_ICON); + widget = (IconWidget)screen.constructWidget(WIDGET_ICON); widget.setX(x); widget.setY(y); widget.setText(iconName); diff --git a/src/main/java/org/lcdproc/lcdjava/IpMenuItem.java b/src/main/java/org/lcdproc/lcdjava/IpMenuItem.java new file mode 100644 index 0000000..f3d2a3f --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/IpMenuItem.java @@ -0,0 +1,109 @@ +package org.lcdproc.lcdjava; + + + + +/** + * IpMenuItem. + *

Allows the user to input an IP number (v4 or v6). When selected, a + * screen comes up that shows an IP number that can be edited - digit by digit + * - via left/right (switch digit) and up/down keys (increase/decrease). + * @author StefanKrupop + */ +public class IpMenuItem extends AbstractMenuItem +{ + private String _value; + private boolean _v6; + + /** + * Constructor. + * @param id the of the action. + * @param menu the Submenu that knows about this MenuItem. + */ + protected IpMenuItem(String id, Submenu menu) + { + super(id, menu); + _value = ""; + _v6 = false; + } + + /** + * Get the menu item type. + * @return the menu item type. + */ + public String getType() + { + return MenuItem.MENUITEM_IP; + } + + /** + * Sets current value of the entry + * @param value current value + */ + public void setValue(String value) { + _value = value; + update(); + } + + /** + * Sets current value of the entry but does not send the change to LCDd + * @param value current value + */ + public void setValueNoUpdate(String value) { + _value = value; + } + + /** + * Gets the currently set ip + * @return value IP in string format + */ + public String getValue() { + return _value; + } + + /** + * Sets if this entry is for v6 IP addresses + * @param isV6 current value + */ + public void setV6(boolean isV6) { + _v6 = isV6; + update(); + } + + @Override + public String getData() { + StringBuilder strb = new StringBuilder(); + strb.append(super.getData()); + strb.append(" -value \""); + strb.append(_value); + strb.append("\" -v6 "); + strb.append(_v6 ? "true" : "false"); + + return strb.toString(); + } + + /** + * Construct a new IpMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @param current the current IP + * @return a new IpMenuItem. + */ + public static IpMenuItem construct(Submenu menu, String text, String current) + { + IpMenuItem menuItem = null; + + try + { + menuItem = (IpMenuItem)menu.constructMenuItem(MenuItem.MENUITEM_IP); + menuItem.setValue(current); + menuItem.setText(text); + } + catch (LCDException e) //NOPMD + { + // Supress, would only get one if we asked for an invalid menu item. + } + + return menuItem; + } +} diff --git a/src/org/boncey/lcdjava/LCD.java b/src/main/java/org/lcdproc/lcdjava/LCD.java similarity index 64% rename from src/org/boncey/lcdjava/LCD.java rename to src/main/java/org/lcdproc/lcdjava/LCD.java index adda1a3..1d72e4a 100644 --- a/src/org/boncey/lcdjava/LCD.java +++ b/src/main/java/org/lcdproc/lcdjava/LCD.java @@ -1,5 +1,6 @@ -package org.boncey.lcdjava; +package org.lcdproc.lcdjava; +import java.awt.event.ActionEvent; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; @@ -10,7 +11,9 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.log4j.Logger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Class to control access to an LCDproc LCDd daemon. @@ -26,28 +29,25 @@ public class LCD implements LCDListener public static final String CVSID = "$Id: LCD.java,v 1.3 2008-07-06 15:38:34 boncey Exp $"; - /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); + private static final Logger _log = LoggerFactory.getLogger(LCD.class); - /** + /** * How often to poll for changes. */ private static final int POLL = 100; - /** + /** * How many times to poll the server before giving up. */ - public static final int POLL_REPEAT = 30; + private static final int POLL_REPEAT = 30; - /** + /** * The pattern for matching a grouping of one or more numbers and . * characters. */ private static final String PATTERN_NUMERIC = "([\\d\\.]+)"; - /** + /** * The pattern for matching the server version. *

All numeric for stable, text for CVS version. */ @@ -64,67 +64,107 @@ public class LCD implements LCDListener " cellwid " + PATTERN_NUMERIC + " cellhgt " + PATTERN_NUMERIC; - /** + /** * The response from LCDd that indicates it didn't understand our command. */ public static final String RESPONSE_ERROR = "huh?"; - /** + /** * The response from LCDd that indicates it is listening to a Screen. */ public static final String RESPONSE_IGNORE = "ignore"; - /** + /** * The response from LCDd that indicates it is ignoring a Screen. */ public static final String RESPONSE_LISTEN = "listen"; - /** + /** * The command to send to the server to initiate communication. */ private static final String CMD_INIT = "hello"; - /** + /** * The command to send to the server to identify ourselves. */ - public static final String CMD_CLIENT_SET = "client_set -name lcdjava/1.0"; + private static final String CMD_CLIENT_SET = "client_set -name "; - /** + /** * The command to send to the server to add a Screen. */ - public static final String CMD_SCREEN_ADD = "screen_add "; + private static final String CMD_SCREEN_ADD = "screen_add "; - /** + /** * The command to send to the server to set (modify) a Screen. */ - public static final String CMD_SCREEN_SET = "screen_set "; + private static final String CMD_SCREEN_SET = "screen_set "; - /** + /** * The command to send to the server to remove a Screen. */ - public static final String CMD_SCREEN_DEL = "screen_del "; + private static final String CMD_SCREEN_DEL = "screen_del "; - /** + /** * The command to send to the server to add a Widget. */ public static final String CMD_WIDGET_ADD = "widget_add "; - /** + /** * The command to send to the server to set (modify) a Widget. */ public static final String CMD_WIDGET_SET = "widget_set "; - /** + /** * The command to send to the server to remove a Widget. */ public static final String CMD_WIDGET_DEL = "widget_del "; - /** + /** + * The command to send to the server to add a menu item. + */ + public static final String CMD_MENU_ADD = "menu_add_item "; + + /** + * The command to send to the server to remove a menu item. + */ + public static final String CMD_MENU_DEL = "menu_del_item "; + + /** + * The command to send to the server to set (modify) a MenuItem. + */ + public static final String CMD_MENU_SET = "menu_set_item "; + + /** + * The command to send to the server to set a menu as main menu + */ + public static final String CMD_MENU_SET_MAIN = "menu_set_main "; + + /** + * The response from LCDd that indicates an action menu item was selected. + */ + private static final String EVENT_SELECT = "select"; + + /** + * The response from LCDd that indicates an menu item was updated. + */ + private static final String EVENT_UPDATE = "update"; + + /** + * The response from LCDd that indicates a slider was moved to the right. + */ + private static final String EVENT_PLUS = "plus"; + + /** + * The response from LCDd that indicates a slider was moved to the left. + */ + private static final String EVENT_MINUS = "minus"; + + /** * The protocol version that we know how to deal with. */ - public static final String PROTOCOL_VERSION = "0.3"; + private static final String PROTOCOL_VERSION = "0.3"; - /** + /** * This is the maximum data that can be sent at once. * This is calculated from the following code in lcdproc-0.4.5/server/sock.c * #define MAXMSG 8192 @@ -137,69 +177,74 @@ public class LCD implements LCDListener */ private static final int MAX_DATA_LENGTH = 7168; - /** + /** * The measurement of a LCD frame, documented as one eight of a * second. */ public static final float FRAME = 0.125f; - /** + /** * The Socket used to talk and listen to the LCDd server. */ private Socket _socket; - /** + /** * The Writer we send all data to the server with. */ private BufferedWriter _out; - /** + /** * Thread that listens for responses from the server in a non-blocking * manner. */ private LCDSocketPoller _poller; - /** + /** * The software version of LCDd. */ private String _version; - - /** + + /** * The protocol version. */ private String _protocolVersion; - - /** + + /** * The width in characters of the LCD device. */ private int _width; - - /** + + /** * The height in characters of the LCD device. */ private int _height; - - /** + + /** * The cell width of the LCD device. */ private int _cellWidth; - - /** + + /** * The cell height of the LCD device. */ private int _cellHeight; - /** + /** * The Map of Screens, indexed by the Screen id. */ - private Map _screens; + private final Map _screens; - /** + /** * The count of screens we have created. *

This is used internally to ensure Screens have unique ids. */ private int _screenCounter; + /** + * The root of the client's menu + */ + private final Submenu _rootMenu; + /** * Public constructor. * @param host the LCDd host. @@ -209,11 +254,25 @@ public class LCD implements LCDListener public LCD(String host, int port) throws LCDException { - _screens = new HashMap(); + this(host, port, "lcdjava/1.0"); + } + + /** + * Public constructor. + * @param host the LCDd host. + * @param port the LCDd port. + * @param clientName the name of this client as shown to the user + * @throws LCDException if there was a problem connecting to the server. + */ + public LCD(String host, int port, String clientName) + throws LCDException + { + _screens = new HashMap<>(); + _rootMenu = new Submenu(this); try { - String response = connect(host, port); + String response = connect(host, port, clientName); if (response == null) { throw new LCDException( @@ -239,7 +298,7 @@ else if (response.startsWith(RESPONSE_ERROR)) shutdown(); throw e; } - + _log.info("Connected"); } /** @@ -250,7 +309,7 @@ public String getVersion() { return _version; } - + /** * Get the protocolVersion. @@ -260,7 +319,7 @@ public String getProtocolVersion() { return _protocolVersion; } - + /** * Get the width. @@ -270,7 +329,7 @@ public int getWidth() { return _width; } - + /** * Get the height. @@ -280,7 +339,7 @@ public int getHeight() { return _height; } - + /** * Get the cellWidth. @@ -290,7 +349,7 @@ public int getCellWidth() { return _cellWidth; } - + /** * Get the cellHeight. @@ -301,7 +360,7 @@ public int getCellHeight() return _cellHeight; } - /** + /** * The listen/ignore state of a Screen has been notified to us by LCDd. * @param screenId the id of the Screen. * @param listening true if the server is listening to us, @@ -309,7 +368,7 @@ public int getCellHeight() */ public void setListenStatus(int screenId, boolean listening) { - Screen screen = (Screen)_screens.get(new Integer(screenId)); + Screen screen = _screens.get(screenId); if (screen != null) { screen.setListening(listening); @@ -323,24 +382,98 @@ public void setListenStatus(int screenId, boolean listening) } } - /** + /** + * The user interacted with a menu + * @param menuId the id of the menu item. + * @param eventType the type of the event that occurred + * @param value the value returned or nullwhen not available + */ + @Override + public void menuAction(String menuId, String eventType, String value) { + String menuParts[] = menuId.split("_"); + StringBuilder id = new StringBuilder(); + MenuItem menu = _rootMenu; + for (String part : menuParts) { + if (!"".equals(part)) { + id.append('_'); + id.append(part); + if (menu instanceof Submenu) { + menu = ((Submenu)menu).getMenuItem(id.toString()); + } else { + break; + } + if (menu == null) { + break; + } + } + } + if (menu != null) { + switch (eventType) { + case LCD.EVENT_SELECT: + if (menu instanceof ActionMenuItem) { + ((ActionMenuItem) menu).notifyActionPerformed(new ActionEvent(menu, ActionEvent.ACTION_PERFORMED, menu.getID())); + } + break; + case LCD.EVENT_UPDATE: + if (menu instanceof CheckboxMenuItem) { + if ("on".equals(value)) { + ((CheckboxMenuItem) menu).setValueNoUpdate(CheckboxMenuItem.CheckboxValue.On); + } else if ("off".equals(value)) { + ((CheckboxMenuItem) menu).setValueNoUpdate(CheckboxMenuItem.CheckboxValue.Off); + } else if ("gray".equals(value)) { + ((CheckboxMenuItem) menu).setValueNoUpdate(CheckboxMenuItem.CheckboxValue.Gray); + } + ((CheckboxMenuItem) menu).notifyActionPerformed(new ActionEvent(menu, ActionEvent.ACTION_PERFORMED, menu.getID())); + } else if (menu instanceof RingMenuItem) { + ((RingMenuItem) menu).setValueNoUpdate(Integer.parseInt(value)); + ((RingMenuItem) menu).notifyActionPerformed(new ActionEvent(menu, ActionEvent.ACTION_PERFORMED, menu.getID())); + } else if (menu instanceof NumericMenuItem) { + ((NumericMenuItem) menu).setValueNoUpdate(Integer.parseInt(value)); + ((NumericMenuItem) menu).notifyActionPerformed(new ActionEvent(menu, ActionEvent.ACTION_PERFORMED, menu.getID())); + } else if (menu instanceof AlphaMenuItem) { + ((AlphaMenuItem) menu).setValueNoUpdate(value); + ((AlphaMenuItem) menu).notifyActionPerformed(new ActionEvent(menu, ActionEvent.ACTION_PERFORMED, menu.getID())); + } else if (menu instanceof IpMenuItem) { + ((IpMenuItem) menu).setValueNoUpdate(value); + ((IpMenuItem) menu).notifyActionPerformed(new ActionEvent(menu, ActionEvent.ACTION_PERFORMED, menu.getID())); + } + break; + case LCD.EVENT_PLUS: + case LCD.EVENT_MINUS: + if (menu instanceof SliderMenuItem) { + int v = Integer.parseInt(value); + if (v != ((SliderMenuItem) menu).getValue()) { + ((SliderMenuItem) menu).setValueNoUpdate(v); + ((NumericMenuItem) menu).notifyActionPerformed(new ActionEvent(menu, ActionEvent.ACTION_PERFORMED, menu.getID())); + } + } + break; + } + } + } + + /** * Shut down the server, terminating any threads. * @throws LCDException in case of a network problem. */ public void shutdown() throws LCDException { + _log.debug("Shutdown requested"); if (_poller != null) { - _poller.shutdown(); - while (_poller.isPolling()) - { - // loop until LCDSocketPoller has stopped polling + _poller.interrupt(); + _log.debug("Waiting for LCDSocketPoller to terminate..."); + try { + _poller.join(); + } catch (InterruptedException e) { + e.printStackTrace(); } } try { + _log.debug("Closing socket"); if (_socket != null && !_socket.isClosed()) { _socket.close(); @@ -352,9 +485,9 @@ public void shutdown() } } - /** + /** * Construct a new (unactivated) Screen. - * @param name the Screeen name. + * @param name the Screen name. * @return the newly constructed Screen. */ public synchronized Screen constructScreen(String name) @@ -362,9 +495,9 @@ public synchronized Screen constructScreen(String name) return constructScreen(name, Screen.PRIORITY_HIDDEN); } - /** + /** * Construct a new (unactivated) Screen. - * @param name the Screeen name. + * @param name the Screen name. * @param priority the screen priority. * @return the newly constructed Screen. */ @@ -373,9 +506,9 @@ public synchronized Screen constructScreen(String name, String priority) return constructScreen(name, priority, false); } - /** + /** * Construct and optionally activate a new Screen. - * @param name the Screeen name. + * @param name the Screen name. * @param priority the screen priority. * @param activate true to activate this Screen immediately, * false to leave in unactivated. @@ -398,14 +531,14 @@ public synchronized Screen constructScreen(String name, return screen; } - /** + /** * Add this screen to the LCD server. *

There is no need to call this, call {@link Screen#activate()} instead. * @param screen the Screen to add. */ protected synchronized void addScreen(Screen screen) { - Integer id = new Integer(screen.getId()); + Integer id = screen.getId(); if (!_screens.containsKey(id)) { _screens.put(id, screen); @@ -414,20 +547,20 @@ protected synchronized void addScreen(Screen screen) } } - /** + /** * Update this screen to the LCD server. * @param screen the Screen to update. */ protected synchronized void updateScreen(Screen screen) { - Integer id = new Integer(screen.getId()); + Integer id = screen.getId(); if (_screens.containsKey(id)) { write(LCD.CMD_SCREEN_SET + screen.getData()); } } - /** + /** * Remove a Screen from the server. * @param screen the Screen to remove. * @return the removed Screen, or null if not removed. @@ -437,7 +570,7 @@ protected synchronized Screen removeScreen(Screen screen) return removeScreen(screen.getId()); } - /** + /** * Remove a Screen from the server. * @param screenId the Screen id to remove. * @return the removed Screen, or null if not removed. @@ -445,10 +578,14 @@ protected synchronized Screen removeScreen(Screen screen) protected synchronized Screen removeScreen(int screenId) { write(CMD_SCREEN_DEL + screenId); - return (Screen)_screens.remove(new Integer(screenId)); + return _screens.remove(screenId); } - /** + public Submenu getRootMenu() { + return _rootMenu; + } + + /** * Parse the init string and split up into groups. * @param init the init string. * @throws LCDException if the protocol version is one we don't know about. @@ -480,14 +617,15 @@ private void parseInitString(String init) } } - /** + /** * Connect to the LCDd server. * @param host the hostname to connect to. * @param port the port to connect to. + * @param clientName the name of this client as shown to the user * @return the response from the server. * @throws IOException in case of a network problem. */ - private String connect(String host, int port) + private String connect(String host, int port, String clientName) throws IOException { _socket = new Socket(host, port); @@ -496,8 +634,7 @@ private String connect(String host, int port) _out = new BufferedWriter(new OutputStreamWriter( _socket.getOutputStream())); _poller = new LCDSocketPoller(in, this); - Thread t = new Thread(_poller); - t.start(); + _poller.start(); write(CMD_INIT); String response = null; @@ -517,12 +654,12 @@ private String connect(String host, int port) } } } - write(CMD_CLIENT_SET); + write(CMD_CLIENT_SET + clientName); return response; } - /** + /** * Write the data to the server. * @param text the text to write. */ @@ -542,7 +679,7 @@ protected void write(String text) } catch (IOException e) { - e.printStackTrace(); + throw new LCDException(e); } } diff --git a/src/org/boncey/lcdjava/LCDException.java b/src/main/java/org/lcdproc/lcdjava/LCDException.java similarity index 85% rename from src/org/boncey/lcdjava/LCDException.java rename to src/main/java/org/lcdproc/lcdjava/LCDException.java index ffdd1b1..3133245 100644 --- a/src/org/boncey/lcdjava/LCDException.java +++ b/src/main/java/org/lcdproc/lcdjava/LCDException.java @@ -1,4 +1,4 @@ -package org.boncey.lcdjava; +package org.lcdproc.lcdjava; /** * Exception that wraps any Exceptions thrown when interacting with LCDProc. @@ -6,20 +6,20 @@ * @author Darren Greaves. * @version $Id: LCDException.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public class LCDException extends Exception +public class LCDException extends RuntimeException { /** * Version details. */ - public static final String CVSID = + public static final String CVSID = "$Id: LCDException.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; - + /** * Constructor. * @param message The error message. * @param t The wrapped exception. - */ + */ public LCDException(String message, Throwable t) { super(message, t); @@ -29,7 +29,7 @@ public LCDException(String message, Throwable t) /** * Constructor. * @param t The wrapped exception. - */ + */ public LCDException(Throwable t) { super(t); @@ -39,7 +39,7 @@ public LCDException(Throwable t) /** * Constructor. * @param message The error message. - */ + */ public LCDException(String message) { super(message); diff --git a/src/org/boncey/lcdjava/LCDListener.java b/src/main/java/org/lcdproc/lcdjava/LCDListener.java similarity index 61% rename from src/org/boncey/lcdjava/LCDListener.java rename to src/main/java/org/lcdproc/lcdjava/LCDListener.java index 78486ce..f695774 100644 --- a/src/org/boncey/lcdjava/LCDListener.java +++ b/src/main/java/org/lcdproc/lcdjava/LCDListener.java @@ -1,7 +1,7 @@ -package org.boncey.lcdjava; +package org.lcdproc.lcdjava; /** - * Interface for listening to the state of a Screen. + * Interface for listening to the state returned from LCDd. *

Copyright (c) 2004-2005 Darren Greaves. * @author Darren Greaves * @version $Id: LCDListener.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ @@ -21,5 +21,13 @@ public interface LCDListener * false if not. */ public void setListenStatus(int screenId, boolean listening); + + /** + * The user interacted with a menu + * @param menuId the id of the menu item. + * @param eventType the type of the event that occured + * @param value the value returned or nullwhen not available + */ + public void menuAction(String menuId, String eventType, String value); } diff --git a/src/main/java/org/lcdproc/lcdjava/LCDSocketPoller.java b/src/main/java/org/lcdproc/lcdjava/LCDSocketPoller.java new file mode 100644 index 0000000..6f1233b --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/LCDSocketPoller.java @@ -0,0 +1,144 @@ +package org.lcdproc.lcdjava; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Thread that listens for data on the LCD socket. + *

Copyright (c) 2004-2005 Darren Greaves. + * @author Darren Greaves + * @version $Id: LCDSocketPoller.java,v 1.3 2008-07-06 15:38:34 boncey Exp $ + */ +public class LCDSocketPoller extends Thread +{ + /** + * Version details. + */ + public static final String CVSID = + "$Id: LCDSocketPoller.java,v 1.3 2008-07-06 15:38:34 boncey Exp $"; + + private static Logger _log = LoggerFactory.getLogger(LCDSocketPoller.class); + + /** + * How often to poll for changes. + */ + private static final int POLL = 100; + + /** + * The Pattern that matches ignore/listen events. + */ + private static final Pattern IGNORE_STATUS = Pattern.compile( + "(ignore|listen)\\s+(\\d+).*"); + + /** + * The Pattern that matches menu events. + */ + private static final Pattern MENU_STATUS = Pattern.compile( + "menuevent\\s+(\\w+)\\s+(\\w+)\\s*(.*)"); + + /** + * The Reader to read data from. + */ + private BufferedReader _in; + + /** + * The last line of data received. + *

Will be null if no data was received. + */ + private String _lastLine; + + /** + * The listener to notify of listen/ignore events. + */ + private LCDListener _listener; + + /** + * Public constructor. + * @param in the BufferedReader that will recieve data from the server. + * @param listener the LCDListener that gets notified of screens being + * listened to or ignored. + */ + public LCDSocketPoller(BufferedReader in, LCDListener listener) + { + _in = in; + _listener = listener; + } + + /** + * Poll the socket every 100 milliseconds. + */ + @Override + public void run() + { + while (!interrupted()) + { + try { + if (!_in.ready()) { + Thread.sleep(POLL); + } + if (_in.ready()) + { + _lastLine = _in.readLine(); + if (_lastLine == null) + continue; + Matcher listenIgnore = IGNORE_STATUS.matcher(_lastLine); + Matcher menuEvent = MENU_STATUS.matcher(_lastLine); + if (_lastLine.startsWith(LCD.RESPONSE_ERROR)) + { + _log.warn("Got a response of " + _lastLine + + " from server"); + } + else if (_listener != null) + { + + if (listenIgnore.matches()) + { + boolean listen = (LCD.RESPONSE_LISTEN.equals( + listenIgnore.group(1))); + try + { + int screenId = Integer.parseInt(listenIgnore.group(2)); + _listener.setListenStatus(screenId, listen); + } + catch (NumberFormatException e) + { + // Ignore + } + } + else if (menuEvent.matches()) + { + _listener.menuAction(menuEvent.group(2), menuEvent.group(1), menuEvent.group(3)); + } + } + } + } + catch (InterruptedException e) + { + interrupt(); + } + catch (IOException e) + { + _log.error("Caught IOException", e); + } + } + + _log.debug("Terminating"); + } + + /** + * Get the last line received non-blocking. + *

Calling this clears the last line received. + * @return the last line received. + */ + synchronized String getLastLine() + { + String ret = _lastLine; + _lastLine = null; + return ret; + } +} diff --git a/src/main/java/org/lcdproc/lcdjava/MenuItem.java b/src/main/java/org/lcdproc/lcdjava/MenuItem.java new file mode 100644 index 0000000..facf998 --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/MenuItem.java @@ -0,0 +1,78 @@ +package org.lcdproc.lcdjava; + + +/** + * Interface for menu items. + * @author Stefan Krupop + */ +public interface MenuItem +{ + /** + * The name of the action item. + */ + public static final String MENUITEM_ACTION = "action"; + + /** + * The name of the checkbox item. + */ + public static final String MENUITEM_CHECKBOX = "checkbox"; + + /** + * The name of the ring item. + */ + public static final String MENUITEM_RING = "ring"; + + /** + * The name of the slider item. + */ + public static final String MENUITEM_SLIDER = "slider"; + + /** + * The name of the numeric item. + */ + public static final String MENUITEM_NUMERIC = "numeric"; + + /** + * The name of the alpha item. + */ + public static final String MENUITEM_ALPHA = "alpha"; + + /** + * The name of the IP item. + */ + public static final String MENUITEM_IP = "ip"; + + /** + * The name of the menu item. + */ + public static final String MENUITEM_MENU = "menu"; + + /** + * Get the menu id. + * @return the menu id. + */ + public String getID(); + + /** + * Get the menu type. + * @return the menu type. + */ + public String getType(); + + /** + * Return the data this item needs to update itself. + * @return the data to update this item. + */ + public String getData(); + + /** + * Add this item to the Menu. + * @return whether or not the item was added successfully. + */ + public boolean activate(); + + /** + * Remove this item. + */ + public void remove(); +} diff --git a/src/org/boncey/lcdjava/NumWidget.java b/src/main/java/org/lcdproc/lcdjava/NumWidget.java similarity index 89% rename from src/org/boncey/lcdjava/NumWidget.java rename to src/main/java/org/lcdproc/lcdjava/NumWidget.java index a81c59f..640546a 100644 --- a/src/org/boncey/lcdjava/NumWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/NumWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Number Widget. @@ -9,7 +7,7 @@ * @author Darren Greaves * @version $Id: NumWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public class NumWidget extends AbstractWidget +public class NumWidget extends AbstractWidget { /** * Version details. @@ -18,16 +16,11 @@ public class NumWidget extends AbstractWidget "$Id: NumWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - - /** * The Widget number. */ private int _number; - /** + /** * The x position. */ private int _x = 1; @@ -42,7 +35,7 @@ protected NumWidget(int id, Screen screen) super(id, screen); } - /** + /** * Get the Widget type. * @return the Widget type. */ @@ -79,7 +72,7 @@ public void setNumber(int number) update(); } - /** + /** * Get the Widget number. * @return the Widget number. */ @@ -88,7 +81,7 @@ public int getNumber() return _number; } - /** + /** * Return the data this Widget needs to update itself. * @return the data to update this Widget. */ @@ -97,7 +90,7 @@ public String getData() return _x + " " + _number; } - /** + /** * Construct a new NumWidget. * @param screen the Screen that owns the Widget. * @param x the x position. diff --git a/src/main/java/org/lcdproc/lcdjava/NumericMenuItem.java b/src/main/java/org/lcdproc/lcdjava/NumericMenuItem.java new file mode 100644 index 0000000..0acaf25 --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/NumericMenuItem.java @@ -0,0 +1,149 @@ +package org.lcdproc.lcdjava; + + + + +/** + * NumericMenuItem. + *

Allows the user to input an integer value. Is visible as a text. When + * selected, a screen comes up that shows the current numeric value, that you + * can edit with the cursor keys and Enter. The number is ended by selecting + * a 'null' input digit. After that the menu returns. + * @author StefanKrupop + */ +public class NumericMenuItem extends AbstractMenuItem +{ + private int _value; + private int _minvalue; + private int _maxvalue; + + /** + * Constructor. + * @param id the of the action. + * @param menu the Submenu that knows about this MenuItem. + */ + protected NumericMenuItem(String id, Submenu menu) + { + super(id, menu); + _value = 0; + _minvalue = 0; + _maxvalue = 100; + } + + /** + * Get the menu item type. + * @return the menu item type. + */ + public String getType() + { + return MENUITEM_NUMERIC; + } + + /** + * Sets current value of the entry + * @param value current value + */ + public void setValue(int value) { + if (value >= _minvalue && value <= _maxvalue) { + _value = value; + update(); + } + } + + /** + * Sets current value of the entry but does not send the change to LCDd + * @param value current value + */ + public void setValueNoUpdate(int value) { + if (value >= _minvalue && value <= _maxvalue) { + _value = value; + } + } + + /** + * Gets the current value of the entry + * @return value of the entry + */ + public int getValue() { + return _value; + } + + /** + * Sets minimum value of the entry + * @param value min value + */ + public void setMinValue(int value) { + _minvalue = value; + if (_value < _minvalue) { + _value = _minvalue; + } + if (_minvalue <= _maxvalue) { + update(); + } + } + + /** + * Sets maximum value of the entry + * @param value max value + */ + public void setMaxValue(int value) { + _maxvalue = value; + if (_value > _maxvalue) { + _value = _maxvalue; + } + if (_maxvalue >= _minvalue) { + update(); + } + } + + @Override + public String getData() { + StringBuilder strb = new StringBuilder(); + strb.append(super.getData()); + strb.append(" -value "); + strb.append(_value); + strb.append(" -minvalue "); + strb.append(_minvalue); + strb.append(" -maxvalue "); + strb.append(_maxvalue); + return strb.toString(); + } + + /** + * Construct a new NumericMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @return a new NumericMenuItem. + */ + public static NumericMenuItem construct(Submenu menu, String text) + { + return construct(menu, text, 0, 100); + } + + /** + * Construct a new NumericMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @param minValue the minimum value of the entry + * @param maxValue the maximum value of the entry + * @return a new NumericMenuItem. + */ + public static NumericMenuItem construct(Submenu menu, String text, int minValue, int maxValue) + { + NumericMenuItem menuItem = null; + + try + { + menuItem = (NumericMenuItem)menu.constructMenuItem(MENUITEM_NUMERIC); + menuItem.setText(text); + menuItem.setMinValue(minValue); + menuItem.setMaxValue(maxValue); + } + catch (LCDException e) //NOPMD + { + // Supress, would only get one if we asked for an invalid menu item. + } + + return menuItem; + } +} diff --git a/src/org/boncey/lcdjava/PositionalWidget.java b/src/main/java/org/lcdproc/lcdjava/PositionalWidget.java similarity index 85% rename from src/org/boncey/lcdjava/PositionalWidget.java rename to src/main/java/org/lcdproc/lcdjava/PositionalWidget.java index f8390ce..4475c7a 100644 --- a/src/org/boncey/lcdjava/PositionalWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/PositionalWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Abstract Widget that deals with x and y dimensions. @@ -8,7 +6,7 @@ * @author Darren Greaves * @version $Id: PositionalWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public abstract class PositionalWidget extends AbstractWidget +public abstract class PositionalWidget extends AbstractWidget { /** * Version details. @@ -17,16 +15,11 @@ public abstract class PositionalWidget extends AbstractWidget "$Id: PositionalWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - - /** * The x position. */ private int _x = 1; - /** + /** * The y position. */ private int _y = 1; @@ -77,7 +70,7 @@ public int getY() return _y; } - /** + /** * Return the data this Widget needs to update itself. * @return the data to update this Widget. */ diff --git a/src/main/java/org/lcdproc/lcdjava/RingMenuItem.java b/src/main/java/org/lcdproc/lcdjava/RingMenuItem.java new file mode 100644 index 0000000..db002a8 --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/RingMenuItem.java @@ -0,0 +1,134 @@ +package org.lcdproc.lcdjava; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + + +/** + * RingMenuItem. + *

Item to display a text and a status indicator. + * The status can be one of the strings specified for the item. + * @author StefanKrupop + */ +public class RingMenuItem extends AbstractMenuItem +{ + private int _value; + private List _items; + + /** + * Constructor. + * @param id the of the action. + * @param menu the Submenu that knows about this MenuItem. + */ + protected RingMenuItem(String id, Submenu menu) + { + super(id, menu); + _items = new ArrayList(); + _value = 0; + } + + /** + * Get the menu item type. + * @return the menu item type. + */ + public String getType() + { + return MENUITEM_RING; + } + + public void setItems(List items) { + _items = items; + _value = 0; + update(); + } + + /** + * Sets current value of the ring + * @param value current value + */ + public void setValue(String value) { + for (int i = 0; i < _items.size(); ++i) { + if (value.equals(_items.get(i))) { + _value = i; + } + } + update(); + } + + /** + * Sets current value of the ring but does not send the change to LCDd + * @param value current value + */ + public void setValueNoUpdate(int value) { + if (value >= 0 && value < _items.size()) { + _value = value; + } + } + + /** + * Gets the current value of the ring + * @return value of the ring + */ + public String getValue() { + return _items.get(_value); + } + + @Override + public String getData() { + StringBuilder strb = new StringBuilder(); + strb.append(super.getData()); + strb.append(" -value "); + strb.append(_value); + strb.append(" -strings \""); + boolean first = true; + for (String item : _items) { + if (!first) { + strb.append('\t'); + } + strb.append(item); + first = false; + } + strb.append('"'); + + return strb.toString(); + } + + /** + * Construct a new RingMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @param items the items in the ring + * @return a new RingMenuItem. + */ + public static RingMenuItem construct(Submenu menu, String text, String... items) + { + return construct(menu, text, Arrays.asList(items)); + } + + /** + * Construct a new RingMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @param items the items in the ring + * @return a new RingMenuItem. + */ + public static RingMenuItem construct(Submenu menu, String text, List items) + { + RingMenuItem menuItem = null; + + try + { + menuItem = (RingMenuItem)menu.constructMenuItem(MENUITEM_RING); + menuItem.setItems(items); + menuItem.setText(text); + } + catch (LCDException e) //NOPMD + { + // Supress, would only get one if we asked for an invalid menu item. + } + + return menuItem; + } +} diff --git a/src/org/boncey/lcdjava/Screen.java b/src/main/java/org/lcdproc/lcdjava/Screen.java similarity index 95% rename from src/org/boncey/lcdjava/Screen.java rename to src/main/java/org/lcdproc/lcdjava/Screen.java index 0ab3902..1e68770 100644 --- a/src/org/boncey/lcdjava/Screen.java +++ b/src/main/java/org/lcdproc/lcdjava/Screen.java @@ -1,8 +1,7 @@ -package org.boncey.lcdjava; +package org.lcdproc.lcdjava; import java.util.HashMap; import java.util.Map; -import org.apache.log4j.Logger; /** A Screen that can contain Widgets. @@ -19,121 +18,116 @@ public class Screen "$Id: Screen.java,v 1.3 2008-07-06 15:38:34 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(Screen.class); - - /** * Set heartbeat to 'Heart icon'. */ public static final String HEARTBEAT_ON = "on"; - /** + /** * Set heartbeat to 'Heart icon'. */ public static final String HEARTBEAT_HEART = "heart"; - /** + /** * Set heartbeat to 'User's choice'. */ public static final String HEARTBEAT_NORMAL = "normal"; - /** + /** * Set heartbeat to 'User's choice'. */ public static final String HEARTBEAT_DEFAULT = "default"; - /** + /** * Set heartbeat to 'None'. */ public static final String HEARTBEAT_OFF = "off"; - /** + /** * Set heartbeat to 'None'. */ public static final String HEARTBEAT_NONE = "none"; - /** + /** * Set heartbeat to 'Rotating slash'. */ public static final String HEARTBEAT_SLASH = "slash"; - /** + /** * The LCD that owns this Screen. */ private LCD _lcd; - /** + /** * The id of the Screen. */ private int _id; - /** + /** * The name of the Screen. */ private String _name; - /** + /** * The priority of the Screen. */ private String _priority; - /** + /** * The duration of the Screen. */ private int _duration; - /** + /** * The width of the Screen. */ private int _width; - /** + /** * The height of the Screen. */ private int _height; - /** + /** * A Map of Widgets added to this Screen. */ - private Map _widgets; + private Map _widgets; - /** + /** * The count of widgets we have created. */ private int _widgetCounter; - /** + /** * Whether or not the server is listening to this screen. */ private boolean _listening; - /** + /** * The heartbeat type. */ private String _heartbeat = HEARTBEAT_OFF; - /** + /** * The priority to set a Screen to "hidden" level. */ public static final String PRIORITY_HIDDEN = "hidden"; - /** + /** * The priority to set a Screen to "background" level. */ public static final String PRIORITY_BACKGROUND = "background"; - /** + /** * The priority to set a Screen to "info" level. */ public static final String PRIORITY_INFO = "info"; - /** + /** * The priority to set a Screen to "foreground" level. */ public static final String PRIORITY_FOREGROUND = "foreground"; - /** + /** * The priority to set a Screen to "alert" level. */ public static final String PRIORITY_ALERT = "alert"; @@ -151,7 +145,7 @@ protected Screen(LCD lcd, int id, String name) _lcd = lcd; _id = id; _name = name; - _widgets = new HashMap(); + _widgets = new HashMap<>(); } /** @@ -166,7 +160,7 @@ protected Screen(LCD lcd, int id) this(lcd, id, null); } - /** + /** * Get the screen id. * @return the screen id. */ @@ -322,7 +316,7 @@ private void update() _lcd.updateScreen(this); } - /** + /** * Activate this Screen. */ public void activate() @@ -330,7 +324,7 @@ public void activate() _lcd.addScreen(this); } - /** + /** * Remove this Screen. */ public void remove() @@ -367,7 +361,7 @@ public String toString() " heartbeat " + _heartbeat; } - /** + /** * Add a Widget to this Screen. * @param widget the Widget to add. * @return whether or not the widget was added successfully. @@ -388,7 +382,7 @@ protected synchronized boolean addWidget(Widget widget) return success; } - /** + /** * Update a Widget to this Screen. * @param widget the Widget to update. * @return whether or not the widget was updated successfully. @@ -408,7 +402,7 @@ protected synchronized boolean updateWidget(Widget widget) return success; } - /** + /** * Delete a Widget from this Screen. * @param widget the Widget to delete. */ @@ -422,7 +416,7 @@ protected synchronized void removeWidget(Widget widget) } } - /** + /** * Create a Widget for this screen. * @param type the Widget type, see {@link Widget} for a list of types. * @return the created Widget. @@ -431,7 +425,7 @@ protected synchronized void removeWidget(Widget widget) public synchronized Widget constructWidget(String type) throws LCDException { - Widget widget = null; + Widget widget; if (type.equals(Widget.WIDGET_TITLE)) { @@ -472,7 +466,7 @@ else if (type.equals(Widget.WIDGET_ICON)) return widget; } - /** + /** * Determine if this Screen is equal to the passed in Object. *

They are considered equal if the passed in Object is not null and is a * Screen object that has the same id as this Screen. @@ -496,7 +490,7 @@ public boolean equals(Object object) return equal; } - /** + /** * Return the hash code for this Screen, simply the id of the Screen. * @return the id of the Screen. */ diff --git a/src/org/boncey/lcdjava/ScrollerWidget.java b/src/main/java/org/lcdproc/lcdjava/ScrollerWidget.java similarity index 92% rename from src/org/boncey/lcdjava/ScrollerWidget.java rename to src/main/java/org/lcdproc/lcdjava/ScrollerWidget.java index 1fb6937..0ea3f18 100644 --- a/src/org/boncey/lcdjava/ScrollerWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/ScrollerWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Scroller Widget. @@ -9,7 +7,7 @@ * @author Darren Greaves * @version $Id: ScrollerWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public class ScrollerWidget extends AbstractWidget +public class ScrollerWidget extends AbstractWidget { /** * Version details. @@ -18,57 +16,52 @@ public class ScrollerWidget extends AbstractWidget "$Id: ScrollerWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - - /** * The Horizontal direction. */ public static final char DIRECTION_HORIZONTAL = 'h'; - /** + /** * The Vertical direction. */ public static final char DIRECTION_VERTICAL = 'v'; - /** + /** * The 'Marquee' direction. *

Will loop around horizontally rather than bounce back and forth. */ public static final char DIRECTION_MARQUEE = 'm'; - /** + /** * The top position of the scroller. */ private int _top = 1; - /** + /** * The bottom position of the scroller. */ private int _bottom = 1; - /** + /** * The left position of the scroller. */ private int _left = 1; - /** + /** * The right position of the scroller. */ private int _right = 1; - /** + /** * The direction to scroll. */ private char _direction; - /** + /** * The speed of the scroller. */ private int _speed; - /** + /** * The Widget text. */ private String _text; @@ -83,13 +76,13 @@ protected ScrollerWidget(int id, Screen screen) super(id, screen); } - /** + /** * Get the Widget type. * @return the Widget type. */ public String getType() { - return Widget.WIDGET_SCROLLER; + return WIDGET_SCROLLER; } /** @@ -102,7 +95,7 @@ public void setText(String text) update(); } - /** + /** * Get the Widget text. * @return the Widget text. */ @@ -230,7 +223,7 @@ public int getSpeed() return _speed; } - /** + /** * Return the data this Widget needs to update itself. * @return the data to update this Widget. */ @@ -240,7 +233,7 @@ public String getData() _direction + " " + _speed + " \"" + stripQuotes(_text) + "\""; } - /** + /** * Construct a new ScrollerWidget. * @param screen the Screen that owns the Widget. * @param left the left position. @@ -266,7 +259,7 @@ public static ScrollerWidget construct(Screen screen, try { widget = (ScrollerWidget)screen.constructWidget( - Widget.WIDGET_SCROLLER); + WIDGET_SCROLLER); widget.setLeft(left); widget.setTop(top); widget.setRight(right); diff --git a/src/main/java/org/lcdproc/lcdjava/SliderMenuItem.java b/src/main/java/org/lcdproc/lcdjava/SliderMenuItem.java new file mode 100644 index 0000000..302881f --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/SliderMenuItem.java @@ -0,0 +1,124 @@ +package org.lcdproc.lcdjava; + + + + +/** + * SliderMenuItem. + *

Is visible as a text. When selected, a screen comes up that shows a + * slider. You can set the slider using the cursor keys. When Enter is + * pressed, the menu returns. + * @author StefanKrupop + */ +public class SliderMenuItem extends NumericMenuItem +{ + private String _mintext; + private String _maxtext; + private int _stepsize; + + /** + * Constructor. + * @param id the of the action. + * @param menu the Submenu that knows about this MenuItem. + */ + protected SliderMenuItem(String id, Submenu menu) + { + super(id, menu); + _mintext = ""; + _maxtext = ""; + _stepsize = 1; + } + + /** + * Get the menu item type. + * @return the menu item type. + */ + public String getType() + { + return MENUITEM_SLIDER; + } + + /** + * Sets label for minimum value of the slider + * @param value text to be shown on left side of slider + */ + public void setMinText(String value) { + _mintext = value; + update(); + } + + /** + * Sets label for maximum value of the slider + * @param value text to be shown on right side of slider + */ + public void setMaxText(String value) { + _maxtext = value; + update(); + } + + /** + * Sets step size of the slider + * @param size step size + */ + public void setStepSize(int size) { + _stepsize = size; + update(); + } + + @Override + public String getData() { + StringBuilder strb = new StringBuilder(); + strb.append(super.getData()); + strb.append(" -stepsize "); + strb.append(_stepsize); + if (!_mintext.isEmpty()) { + strb.append(" -mintext \""); + strb.append(_mintext); + strb.append('"'); + } + if (!_maxtext.isEmpty()) { + strb.append(" -maxtext \""); + strb.append(_maxtext); + strb.append('"'); + } + return strb.toString(); + } + + /** + * Construct a new SliderMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @return a new SliderMenuItem. + */ + public static SliderMenuItem construct(Submenu menu, String text) + { + return construct(menu, text, 0, 100); + } + + /** + * Construct a new SliderMenuItem. + * @param menu the Submenu that owns the menu item. + * @param text the menu text. + * @param minValue the minimum value of the slider + * @param maxValue the maximum value of the slider + * @return a new SliderMenuItem. + */ + public static SliderMenuItem construct(Submenu menu, String text, int minValue, int maxValue) + { + SliderMenuItem menuItem = null; + + try + { + menuItem = (SliderMenuItem)menu.constructMenuItem(MENUITEM_SLIDER); + menuItem.setText(text); + menuItem.setMinValue(minValue); + menuItem.setMaxValue(maxValue); + } + catch (LCDException e) //NOPMD + { + // Supress, would only get one if we asked for an invalid menu item. + } + + return menuItem; + } +} diff --git a/src/org/boncey/lcdjava/StringWidget.java b/src/main/java/org/lcdproc/lcdjava/StringWidget.java similarity index 89% rename from src/org/boncey/lcdjava/StringWidget.java rename to src/main/java/org/lcdproc/lcdjava/StringWidget.java index 3cb785d..f97530d 100644 --- a/src/org/boncey/lcdjava/StringWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/StringWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * String Widget. @@ -9,7 +7,7 @@ * @author Darren Greaves * @version $Id: StringWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public class StringWidget extends PositionalWidget +public class StringWidget extends PositionalWidget { /** * Version details. @@ -18,11 +16,6 @@ public class StringWidget extends PositionalWidget "$Id: StringWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - - /** * The Widget text. */ private String _text; @@ -37,7 +30,7 @@ protected StringWidget(int id, Screen screen) super(id, screen); } - /** + /** * Get the Widget type. * @return the Widget type. */ @@ -56,7 +49,7 @@ public void setText(String text) update(); } - /** + /** * Get the Widget text. * @return the Widget text. */ @@ -65,7 +58,7 @@ public String getText() return _text; } - /** + /** * Return the data this Widget needs to update itself. * @return the data to update this Widget. */ @@ -74,7 +67,7 @@ public String getData() return super.getData() + "\"" + stripQuotes(_text) + "\""; } - /** + /** * Construct a new StringWidget. * @param screen the Screen that owns the Widget. * @param x the x position. diff --git a/src/main/java/org/lcdproc/lcdjava/Submenu.java b/src/main/java/org/lcdproc/lcdjava/Submenu.java new file mode 100644 index 0000000..863df41 --- /dev/null +++ b/src/main/java/org/lcdproc/lcdjava/Submenu.java @@ -0,0 +1,214 @@ +package org.lcdproc.lcdjava; + +import java.util.HashMap; +import java.util.Map; + + +/** + * Submenu. + *

Displays the submenus name with tailing submenu marker + * @author StefanKrupop + */ +public class Submenu extends AbstractMenuItem +{ + /** + * The LCD that owns this Submenu. + */ + private LCD _lcd; + + /** + * A Map of MenuItems added to this Submenu. + */ + private Map _menuItems; + + /** + * The count of menu items we have created. + */ + private int _menuItemCounter; + + /** + * Constructor. + * @param lcd the LCD server object. + * @param id the of the submenu. + * @param menu the Submenu that knows about this MenuItem. + */ + protected Submenu(LCD lcd, String id, Submenu menu) + { + super(id, menu); + _lcd = lcd; + _menuItems = new HashMap(); + } + + public Submenu(LCD lcd) { + this(lcd, "", null); + _menu = this; + } + + /** + * Get the menu item type. + * @return the menu item type. + */ + public String getType() + { + return MENUITEM_MENU; + } + + /** + * Construct a new Submenu. + * @param menu the Submenu that owns the submenu. + * @param text the menu text. + * @return a new Submenu. + */ + public static Submenu construct(Submenu menu, String text) + { + Submenu menuItem = null; + + try + { + menuItem = (Submenu)menu.constructMenuItem(MENUITEM_MENU); + menuItem.setText(text); + } + catch (LCDException e) //NOPMD + { + // Supress, would only get one if we asked for an invalid menu item. + } + + return menuItem; + } + + /** + * Create a MenuItem for this Submenu. + * @param type the menu item type, see {@link MenuItem} for a list of types. + * @return the created MenuItem. + * @throws LCDException if the menu item type is not recognized. + */ + public MenuItem constructMenuItem(String type) + throws LCDException + { + MenuItem item; + + synchronized (this) { + if (type.equals(MENUITEM_MENU)) + { + item = new Submenu(_lcd, getID() + "_" + Integer.toString(_menuItemCounter), this); + } + else if (type.equals(MENUITEM_ACTION)) + { + item = new ActionMenuItem(getID() + "_" + Integer.toString(_menuItemCounter), this); + } + else if (type.equals(MENUITEM_CHECKBOX)) + { + item = new CheckboxMenuItem(getID() + "_" + Integer.toString(_menuItemCounter), this); + } + else if (type.equals(MENUITEM_RING)) + { + item = new RingMenuItem(getID() + "_" + Integer.toString(_menuItemCounter), this); + } + else if (type.equals(MENUITEM_SLIDER)) + { + item = new SliderMenuItem(getID() + "_" + Integer.toString(_menuItemCounter), this); + } + else if (type.equals(MENUITEM_NUMERIC)) + { + item = new NumericMenuItem(getID() + "_" + Integer.toString(_menuItemCounter), this); + } + else if (type.equals(MENUITEM_ALPHA)) + { + item = new AlphaMenuItem(getID() + "_" + Integer.toString(_menuItemCounter), this); + } + else if (type.equals(MENUITEM_IP)) + { + item = new IpMenuItem(getID() + "_" + Integer.toString(_menuItemCounter), this); + } + else + { + throw new LCDException( + "Unable to create a menu item of type " + type); + } + + _menuItemCounter++; + } + + return item; + } + + /** + * Add a MenuItem to this Submenu. + * @param item the MenuItem to add. + * @return whether or not the menu item was added successfully. + */ + protected boolean addItem(MenuItem item) + { + boolean success = false; + + String itemId = item.getID(); + synchronized (this) { + if (!_menuItems.containsKey(itemId)) + { + _menuItems.put(itemId, item); + _lcd.write(LCD.CMD_MENU_ADD + "\"" + _id + "\"" + " " + itemId + + " " + item.getType() + " \"\""); + success = updateItem(item); + } + } + + return success; + } + + /** + * Update a MenuItem on this Submenu. + * @param item the MenuItem to update. + * @return whether or not the MenuItem was updated successfully. + */ + protected boolean updateItem(MenuItem item) + { + boolean success = false; + + String itemId = item.getID(); + synchronized (this) { + if (_menuItems.containsKey(itemId)) + { + _lcd.write(LCD.CMD_MENU_SET + "\"" + _id + "\"" + " " + itemId + + " " + item.getData()); + success = true; + } + } + + return success; + } + + /** + * Delete a MenuItem from this Submenu. + * @param item the MenuItem to delete. + */ + protected void removeItem(MenuItem item) + { + String itemId = item.getID(); + synchronized (this) { + if (_menuItems.containsKey(itemId)) + { + _lcd.write(LCD.CMD_MENU_DEL + "\"" + _id + "\"" + " " + itemId); + _menuItems.remove(itemId); + } + } + } + + /** + * Return menu item with the given id + * @param id the id of the menu item + * @return the MenuItem or null when the ID does not exist + */ + public MenuItem getMenuItem(String id) { + return _menuItems.get(id); + } + + /** + * Sets this menu as the current main menu + */ + public void setAsMainMenu() { + synchronized (this) { + _lcd.write(LCD.CMD_MENU_SET_MAIN + "\"" + _id + "\""); + } + } +} + diff --git a/src/org/boncey/lcdjava/TitleWidget.java b/src/main/java/org/lcdproc/lcdjava/TitleWidget.java similarity index 82% rename from src/org/boncey/lcdjava/TitleWidget.java rename to src/main/java/org/lcdproc/lcdjava/TitleWidget.java index 6d1d790..57dcec9 100644 --- a/src/org/boncey/lcdjava/TitleWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/TitleWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Title Widget. @@ -9,7 +7,7 @@ * @author Darren Greaves * @version $Id: TitleWidget.java,v 1.3 2008-07-06 15:38:34 boncey Exp $ */ -public class TitleWidget extends AbstractWidget +public class TitleWidget extends AbstractWidget { /** * Version details. @@ -18,11 +16,6 @@ public class TitleWidget extends AbstractWidget "$Id: TitleWidget.java,v 1.3 2008-07-06 15:38:34 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - - /** * The Widget text. */ private String _text; @@ -37,13 +30,13 @@ protected TitleWidget(int id, Screen screen) super(id, screen); } - /** + /** * Get the Widget type. * @return the Widget type. */ public String getType() { - return Widget.WIDGET_TITLE; + return WIDGET_TITLE; } /** @@ -56,7 +49,7 @@ public void setText(String text) update(); } - /** + /** * Get the Widget text. * @return the Widget text. */ @@ -65,7 +58,7 @@ public String getText() return _text; } - /** + /** * Return the data this Widget needs to update itself. * @return the data to update this Widget. */ @@ -74,7 +67,7 @@ public String getData() return "\"" + stripQuotes(_text) + "\""; } - /** + /** * Construct a new TitleWidget. * @param screen the Screen that owns the Widget. * @param text the widget text. @@ -86,7 +79,7 @@ public static TitleWidget construct(Screen screen, String text) try { - widget = (TitleWidget)screen.constructWidget(Widget.WIDGET_TITLE); + widget = (TitleWidget)screen.constructWidget(WIDGET_TITLE); widget.setText(text); } catch (LCDException e) diff --git a/src/org/boncey/lcdjava/VBarWidget.java b/src/main/java/org/lcdproc/lcdjava/VBarWidget.java similarity index 87% rename from src/org/boncey/lcdjava/VBarWidget.java rename to src/main/java/org/lcdproc/lcdjava/VBarWidget.java index 8147d68..8116f5e 100644 --- a/src/org/boncey/lcdjava/VBarWidget.java +++ b/src/main/java/org/lcdproc/lcdjava/VBarWidget.java @@ -1,6 +1,4 @@ -package org.boncey.lcdjava; - -import org.apache.log4j.Logger; +package org.lcdproc.lcdjava; /** * Vertical Bar Widget. @@ -9,7 +7,7 @@ * @author Darren Greaves * @version $Id: VBarWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ */ -public class VBarWidget extends BarWidget +public class VBarWidget extends BarWidget { /** * Version details. @@ -17,11 +15,6 @@ public class VBarWidget extends BarWidget public static final String CVSID = "$Id: VBarWidget.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; - /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCD.class); - /** * Constructor. * @param id the of the Widget. @@ -32,7 +25,7 @@ protected VBarWidget(int id, Screen screen) super(id, screen); } - /** + /** * Get the Widget type. * @return the Widget type. */ @@ -41,7 +34,7 @@ public String getType() return Widget.WIDGET_VBAR; } - /** + /** * Construct a new VBarWidget. * @param screen the Screen that owns the Widget. * @param x the x position. diff --git a/src/org/boncey/lcdjava/Widget.java b/src/main/java/org/lcdproc/lcdjava/Widget.java similarity index 98% rename from src/org/boncey/lcdjava/Widget.java rename to src/main/java/org/lcdproc/lcdjava/Widget.java index ec48eca..f4e87a5 100644 --- a/src/org/boncey/lcdjava/Widget.java +++ b/src/main/java/org/lcdproc/lcdjava/Widget.java @@ -1,4 +1,4 @@ -package org.boncey.lcdjava; +package org.lcdproc.lcdjava; /** * Interface for Widgets. diff --git a/src/org/boncey/lcdjava/idler/AbstractIdler.java b/src/main/java/org/lcdproc/lcdjava/idler/AbstractIdler.java similarity index 84% rename from src/org/boncey/lcdjava/idler/AbstractIdler.java rename to src/main/java/org/lcdproc/lcdjava/idler/AbstractIdler.java index 5445b26..11073a9 100644 --- a/src/org/boncey/lcdjava/idler/AbstractIdler.java +++ b/src/main/java/org/lcdproc/lcdjava/idler/AbstractIdler.java @@ -1,7 +1,6 @@ -package org.boncey.lcdjava.idler; +package org.lcdproc.lcdjava.idler; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.LCDException; +import org.lcdproc.lcdjava.LCDException; import org.w3c.dom.Element; /** @@ -19,11 +18,6 @@ public abstract class AbstractIdler implements Idler "$Id: AbstractIdler.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(AbstractIdler.class); - - /** * The Idler name. */ private String _name; @@ -56,7 +50,7 @@ public String toString() return "Name = " + _name; } - /** + /** * Get the name to display. * @return the name to display. */ diff --git a/src/org/boncey/lcdjava/idler/ExecIdler.java b/src/main/java/org/lcdproc/lcdjava/idler/ExecIdler.java similarity index 92% rename from src/org/boncey/lcdjava/idler/ExecIdler.java rename to src/main/java/org/lcdproc/lcdjava/idler/ExecIdler.java index 5408b51..cb43481 100644 --- a/src/org/boncey/lcdjava/idler/ExecIdler.java +++ b/src/main/java/org/lcdproc/lcdjava/idler/ExecIdler.java @@ -1,10 +1,12 @@ -package org.boncey.lcdjava.idler; +package org.lcdproc.lcdjava.idler; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.LCDException; + +import org.lcdproc.lcdjava.LCDException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Element; /** @@ -21,42 +23,39 @@ public class ExecIdler extends AbstractIdler public static final String CVSID = "$Id: ExecIdler.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; - /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(ExecIdler.class); + private static Logger _log = LoggerFactory.getLogger(ExecIdler.class); - /** + /** * The size of the buffer the program's output is read into. */ private static final int BUFF_SIZE = 4000; - /** + /** * Pad the display to enhance readability. */ private static final String PADDING = " *** "; - /** + /** * The text to display. */ - private StringBuffer _text = new StringBuffer(); + private final StringBuffer _text = new StringBuffer(); - /** + /** * The name of the program to execute. */ private String _program; - /** + /** * Does this Idler have anything to display? */ private boolean _valid; - /** + /** * How often to update this feed in minutes. */ private int _update; - /** + /** * The Runtime object. */ private static Runtime _runtime = Runtime.getRuntime(); @@ -87,7 +86,7 @@ public ExecIdler(Element config, String name) thread.start(); } - /** + /** * Re-read the feed from the given URL. * @return true if success, false otherwise. */ @@ -127,7 +126,7 @@ private boolean execute() return success; } - /** + /** * Is there any text to display? * @return true if there is anything to display, * false otherwise. @@ -137,7 +136,7 @@ public boolean isValid() return _valid; } - /** + /** * Get the text to display. * @return the text to display. */ @@ -161,8 +160,8 @@ public String toString() { return "Program = " + _program + "; Valid = " + _valid; } - - /** + + /** * Thread for handling periodic updates. * @author Darren Greaves */ @@ -175,7 +174,7 @@ public ExecUpdater() { } - /** + /** * Update the feed. */ public void run() diff --git a/src/org/boncey/lcdjava/idler/Idler.java b/src/main/java/org/lcdproc/lcdjava/idler/Idler.java similarity index 97% rename from src/org/boncey/lcdjava/idler/Idler.java rename to src/main/java/org/lcdproc/lcdjava/idler/Idler.java index bc5a37f..8da1fa8 100644 --- a/src/org/boncey/lcdjava/idler/Idler.java +++ b/src/main/java/org/lcdproc/lcdjava/idler/Idler.java @@ -1,4 +1,4 @@ -package org.boncey.lcdjava.idler; +package org.lcdproc.lcdjava.idler; /** * Interface for anything that wants to display idle information diff --git a/src/org/boncey/lcdjava/idler/IdlerLoader.java b/src/main/java/org/lcdproc/lcdjava/idler/IdlerLoader.java similarity index 91% rename from src/org/boncey/lcdjava/idler/IdlerLoader.java rename to src/main/java/org/lcdproc/lcdjava/idler/IdlerLoader.java index 4ef967b..7b9d7c3 100644 --- a/src/org/boncey/lcdjava/idler/IdlerLoader.java +++ b/src/main/java/org/lcdproc/lcdjava/idler/IdlerLoader.java @@ -1,14 +1,15 @@ -package org.boncey.lcdjava.idler; +package org.lcdproc.lcdjava.idler; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.LCDException; -import org.boncey.lcdjava.ScrollerWidget; -import org.boncey.lcdjava.util.WidgetTimer; + +import org.lcdproc.lcdjava.LCDException; +import org.lcdproc.lcdjava.ScrollerWidget; +import org.lcdproc.lcdjava.util.WidgetTimer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -22,10 +23,7 @@ */ public class IdlerLoader { - /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(IdlerLoader.class); + private static Logger _log = LoggerFactory.getLogger(IdlerLoader.class); /** * Version details. @@ -33,42 +31,42 @@ public class IdlerLoader public static final String CVSID = "$Id: IdlerLoader.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; - /** + /** * XML config value. */ private static final String IDLER_ELEMENT = "idler"; - /** + /** * XML config value. */ private static final String CLASSNAME = "className"; - /** + /** * XML config value. */ private static final String NAME = "name"; - /** + /** * The amount of milliseconds in a second. */ public static final int ONE_SECOND = 1000; - /** + /** * The amount of seconds in a minute. */ public static final int ONE_MINUTE = 60; - /** + /** * The timer for controlling display of each Idler. */ private WidgetTimer _timer; - /** + /** * The WidgetUpdater that mananges displaying of the Idlers. */ private IdlerWidgetUpdater _updater; - /** + /** * Default constructor, initialise. * @param doc the XML Document. * @param scroller the Widget to display each Idler upon. @@ -90,7 +88,7 @@ public IdlerLoader(Document doc, load(config, scroller, name); } - /** + /** * Default constructor, initialise. * @param config the XML config. * @param scroller the Widget to display each Idler upon. @@ -105,7 +103,7 @@ public IdlerLoader(Element config, load(config, scroller, name); } - /** + /** * Load the Idlers from the config file. * @param config the XML config. * @param scroller the Widget to display each Idler upon. @@ -121,11 +119,6 @@ private void load(Element config, if (idlers.size() > 0) { - for (Iterator i = idlers.iterator(); i.hasNext();) - { - Idler idler = (Idler)i.next(); - } - _updater = new IdlerWidgetUpdater(scroller, name, idlers); _timer = new WidgetTimer(_updater, ONE_MINUTE); @@ -134,7 +127,7 @@ private void load(Element config, } } - /** + /** * IdlerLoader is being destroyed, kill off any threads. */ public void destroy() @@ -153,7 +146,7 @@ public void next() _updater.updateWidget(_timer); } - /** + /** * Load the configs and store the classes in the internal Map. * @param config the Element that holds the config. * @return a List of Idlers. @@ -162,10 +155,10 @@ public void next() private List loadConfig(Element config) throws LCDException { - List idlers = new ArrayList(); + List idlers = new ArrayList<>(); NodeList nodes = config.getChildNodes(); - for (int i = 0; i < nodes.getLength(); i++) + for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); @@ -237,7 +230,7 @@ private List loadConfig(Element config) return idlers; } - /** + /** * Instantiate a new Idler. * @param moduleConfig the Element that holds the config. * @param name the name of the Idler. @@ -266,9 +259,8 @@ private Idler loadIdler(Element moduleConfig, String name, String className) Object[] args = new Object[] {moduleConfig, name}; Class c = Class.forName(className); + @SuppressWarnings("unchecked") Constructor constructor = c.getConstructor(types); - Idler idler = (Idler)constructor.newInstance(args); - - return idler; + return (Idler)constructor.newInstance(args); } } diff --git a/src/org/boncey/lcdjava/idler/IdlerWidgetUpdater.java b/src/main/java/org/lcdproc/lcdjava/idler/IdlerWidgetUpdater.java similarity index 86% rename from src/org/boncey/lcdjava/idler/IdlerWidgetUpdater.java rename to src/main/java/org/lcdproc/lcdjava/idler/IdlerWidgetUpdater.java index 9959c89..1dd42af 100644 --- a/src/org/boncey/lcdjava/idler/IdlerWidgetUpdater.java +++ b/src/main/java/org/lcdproc/lcdjava/idler/IdlerWidgetUpdater.java @@ -1,12 +1,12 @@ -package org.boncey.lcdjava.idler; +package org.lcdproc.lcdjava.idler; + +import org.lcdproc.lcdjava.LCD; +import org.lcdproc.lcdjava.ScrollerWidget; +import org.lcdproc.lcdjava.Widget; +import org.lcdproc.lcdjava.util.WidgetTimer; +import org.lcdproc.lcdjava.util.WidgetUpdater; import java.util.List; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.LCD; -import org.boncey.lcdjava.ScrollerWidget; -import org.boncey.lcdjava.util.WidgetTimer; -import org.boncey.lcdjava.util.WidgetUpdater; -import org.boncey.lcdjava.Widget; /** * ScrollerWidget that updates a Widget with the text of the provided idler. @@ -23,31 +23,26 @@ public class IdlerWidgetUpdater implements WidgetUpdater "$Id: IdlerWidgetUpdater.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(IdlerWidgetUpdater.class); - - /** * The ScrollerWidget we will be updating with the idler content. */ private ScrollerWidget _content; - /** + /** * The ScrollerWidget we will be updating with the idler name. */ private ScrollerWidget _name; - /** + /** * The list of Idler objects to update. */ private List _idlers; - /** + /** * The index of the last Idler displayed. */ private int _index = -1; - /** + /** * Store the currently displayed text so we don't update if it hasn't * changed (only happens if we have one idler or two identical ones in a * row). @@ -69,7 +64,7 @@ public IdlerWidgetUpdater(ScrollerWidget content, _idlers = idlers; } - /** + /** * Update the Widget by showing the next Idler. * @param timer the WidgetTimer. */ @@ -103,7 +98,7 @@ public void updateWidget(WidgetTimer timer) } } - /** + /** * Retrurn the wrapped Widget. * @return the Widget. */ diff --git a/src/org/boncey/lcdjava/idler/StaticTextIdler.java b/src/main/java/org/lcdproc/lcdjava/idler/StaticTextIdler.java similarity index 86% rename from src/org/boncey/lcdjava/idler/StaticTextIdler.java rename to src/main/java/org/lcdproc/lcdjava/idler/StaticTextIdler.java index 747be54..ae8ffd3 100644 --- a/src/org/boncey/lcdjava/idler/StaticTextIdler.java +++ b/src/main/java/org/lcdproc/lcdjava/idler/StaticTextIdler.java @@ -1,7 +1,6 @@ -package org.boncey.lcdjava.idler; +package org.lcdproc.lcdjava.idler; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.LCDException; +import org.lcdproc.lcdjava.LCDException; import org.w3c.dom.Element; /** @@ -19,11 +18,6 @@ public class StaticTextIdler extends AbstractIdler "$Id: StaticTextIdler.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(StaticTextIdler.class); - - /** * The text to display. */ private String _text; @@ -42,7 +36,7 @@ public StaticTextIdler(Element config, String name) _text = config.getAttribute("text"); } - /** + /** * Get the text to display. * @return the text to display. */ @@ -58,7 +52,7 @@ public void destroy() { } - /** + /** * Is there any text to display? * @return true if there is anything to display, * false otherwise. @@ -76,5 +70,5 @@ public String toString() { return "Text = " + _text; } - + } diff --git a/src/org/boncey/lcdjava/idler/package.html b/src/main/java/org/lcdproc/lcdjava/idler/package.html similarity index 100% rename from src/org/boncey/lcdjava/idler/package.html rename to src/main/java/org/lcdproc/lcdjava/idler/package.html diff --git a/src/org/boncey/lcdjava/package.html b/src/main/java/org/lcdproc/lcdjava/package.html similarity index 100% rename from src/org/boncey/lcdjava/package.html rename to src/main/java/org/lcdproc/lcdjava/package.html diff --git a/src/org/boncey/lcdjava/util/ClockWidgetUpdater.java b/src/main/java/org/lcdproc/lcdjava/util/ClockWidgetUpdater.java similarity index 84% rename from src/org/boncey/lcdjava/util/ClockWidgetUpdater.java rename to src/main/java/org/lcdproc/lcdjava/util/ClockWidgetUpdater.java index 39ea2c2..f2c2c1d 100644 --- a/src/org/boncey/lcdjava/util/ClockWidgetUpdater.java +++ b/src/main/java/org/lcdproc/lcdjava/util/ClockWidgetUpdater.java @@ -1,10 +1,10 @@ -package org.boncey.lcdjava.util; +package org.lcdproc.lcdjava.util; + +import org.lcdproc.lcdjava.StringWidget; +import org.lcdproc.lcdjava.Widget; import java.text.DateFormat; import java.util.Date; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.StringWidget; -import org.boncey.lcdjava.Widget; /** * StringWidget that updates with the current time. @@ -21,16 +21,11 @@ public class ClockWidgetUpdater implements WidgetUpdater "$Id: ClockWidgetUpdater.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(ClockWidgetUpdater.class); - - /** * The StringWidget we will be updating. */ private StringWidget _widget; - /** + /** * Store the time as a String so we only write it when it has changed. */ private String _timeString; @@ -44,7 +39,7 @@ public ClockWidgetUpdater(StringWidget widget) _widget = widget; } - /** + /** * Update the clock. * @param timer the WidgetTimer. */ @@ -59,7 +54,7 @@ public void updateWidget(WidgetTimer timer) } } - /** + /** * Retrurn the wrapped Widget. * @return the Widget. */ diff --git a/src/org/boncey/lcdjava/util/ScreenThread.java b/src/main/java/org/lcdproc/lcdjava/util/ScreenThread.java similarity index 92% rename from src/org/boncey/lcdjava/util/ScreenThread.java rename to src/main/java/org/lcdproc/lcdjava/util/ScreenThread.java index a585513..18b6b80 100644 --- a/src/org/boncey/lcdjava/util/ScreenThread.java +++ b/src/main/java/org/lcdproc/lcdjava/util/ScreenThread.java @@ -1,10 +1,10 @@ -package org.boncey.lcdjava.util; +package org.lcdproc.lcdjava.util; + +import org.lcdproc.lcdjava.Screen; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.Screen; /** * A Runnable class to display a Screen for a specified time period. @@ -21,52 +21,47 @@ public class ScreenThread implements Runnable "$Id: ScreenThread.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(ScreenThread.class); - - /** * The screen we are displaying. */ private Screen _screen; - /** + /** * How long to display the screen for. */ private int _timeout; - /** + /** * The priority to set when displaying. */ private String _priority; - /** + /** * Flag that tracks if we are displaying or not. */ private boolean _displaying; - /** + /** * Flag that tracks if we are alive or not. */ private boolean _alive = true; - /** + /** * Flag that tracks whether we should die after the first timeout or not. */ private boolean _dieOnTimeout; - /** + /** * A List of TimeoutHook objects that want to be notified when the thread * times out. */ - private List _timeoutHooks; + private List _timeoutHooks; - /** + /** * How many times to count down before calling stop. */ private int _step; - /** + /** * How often to poll for changes. */ private static final int POLL = 100; @@ -88,7 +83,7 @@ public ScreenThread(Screen screen, _priority = priority; _dieOnTimeout = dieOnTimeout; - _timeoutHooks = new ArrayList(); + _timeoutHooks = new ArrayList<>(); } /** @@ -102,7 +97,7 @@ public ScreenThread(Screen screen, int timeout, String priority) this(screen, timeout, priority, false); } - /** + /** * Add a TimeoutHook object. * @param hook the TimeoutHook. */ @@ -111,7 +106,7 @@ public void addTimeoutHook(TimeoutHook hook) _timeoutHooks.add(hook); } - /** + /** * Remove a TimeoutHook object. * @param hook the TimeoutHook. * @return true if the TimeoutHook was removed, @@ -122,7 +117,7 @@ public boolean removeTimeoutHook(TimeoutHook hook) return _timeoutHooks.remove(hook); } - /** + /** * Switch to the specified priority for the specified time period. */ public void run() @@ -139,7 +134,7 @@ public void run() if (_step > 0) { _step--; - if (_priority != _screen.getPriority()) + if (!_priority.equals(_screen.getPriority())) { start(); } @@ -159,7 +154,7 @@ public void run() } } - /** + /** * Switch to the specified priority (display the screen). */ private void start() @@ -167,7 +162,7 @@ private void start() _screen.setPriority(_priority); } - /** + /** * Set counter to zero then hide the screen. *

This triggers a timeout and any TimeoutHooks are notified. */ @@ -177,7 +172,7 @@ private void stop() hide(); } - /** + /** * Switch to the hidden priority (hide the screen). *

Explicitly hide the screen without triggering a timeout. */ @@ -187,7 +182,7 @@ public void hide() _screen.setPriority(Screen.PRIORITY_HIDDEN); } - /** + /** * Tell this thread to die gracefully. */ public void destroy() @@ -195,7 +190,7 @@ public void destroy() _alive = false; } - /** + /** * Are we displaying now? * @return true if displaying, false otherwise. */ @@ -204,7 +199,7 @@ public boolean isDisplaying() return _displaying; } - /** + /** * Tell the screen to display for the specified timeout. */ public void display() @@ -213,7 +208,7 @@ public void display() _displaying = true; } - /** + /** * Get the Screen we are wrapping. * @return the Screen we are wrapping. */ @@ -222,7 +217,7 @@ public Screen getScreen() return _screen; } - /** + /** * Call timeout for each TimeoutHook. */ private void timeout() @@ -251,7 +246,7 @@ public String toString() "; screen = " + _screen; } - /** + /** * Determine if this ScreenThread is equal to the passed in Object. *

They are considered equal if the passed in Object is not null and is a * ScreenThread object that has the same Screen, priority and timeout value @@ -276,7 +271,7 @@ public boolean equals(Object object) return equal; } - /** + /** * Return the hash code for this ScreenThread. *

The hash is generated from a String representation of the Screen, * priority and timeout value. diff --git a/src/org/boncey/lcdjava/util/TimeoutHook.java b/src/main/java/org/lcdproc/lcdjava/util/TimeoutHook.java similarity index 94% rename from src/org/boncey/lcdjava/util/TimeoutHook.java rename to src/main/java/org/lcdproc/lcdjava/util/TimeoutHook.java index 81800cb..f60292d 100644 --- a/src/org/boncey/lcdjava/util/TimeoutHook.java +++ b/src/main/java/org/lcdproc/lcdjava/util/TimeoutHook.java @@ -1,4 +1,4 @@ -package org.boncey.lcdjava.util; +package org.lcdproc.lcdjava.util; /** * Hook called when a ScreenThread times out. diff --git a/src/org/boncey/lcdjava/util/WidgetTimer.java b/src/main/java/org/lcdproc/lcdjava/util/WidgetTimer.java similarity index 89% rename from src/org/boncey/lcdjava/util/WidgetTimer.java rename to src/main/java/org/lcdproc/lcdjava/util/WidgetTimer.java index 7456699..986ae14 100644 --- a/src/org/boncey/lcdjava/util/WidgetTimer.java +++ b/src/main/java/org/lcdproc/lcdjava/util/WidgetTimer.java @@ -1,7 +1,6 @@ -package org.boncey.lcdjava.util; +package org.lcdproc.lcdjava.util; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.Widget; +import org.lcdproc.lcdjava.Widget; /** * A Runnable class to update a Widget on a specified time period. @@ -18,21 +17,16 @@ public class WidgetTimer implements Runnable "$Id: WidgetTimer.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(WidgetTimer.class); - - /** * The class that holds a Widget for updating. */ private WidgetUpdater _updater; - /** + /** * Flag that tracks if we are alive or not. */ private boolean _alive = true; - /** + /** * How long to display the widget for (in milliseconds). */ private int _timeout; @@ -48,7 +42,7 @@ public WidgetTimer(WidgetUpdater updater, int timeout) _timeout = timeout; } - /** + /** * Switch to the specified priority for the specified time period. */ public void run() @@ -67,7 +61,7 @@ public void run() } } - /** + /** * Return the wrapped Widget. * @return the Widget. */ @@ -76,7 +70,7 @@ public Widget getWidget() return _updater.getWidget(); } - /** + /** * Alter the timeout value and wake up the thread. * @param timeout the new timeout value. */ @@ -86,7 +80,7 @@ public synchronized void setTimeout(int timeout) notify(); } - /** + /** * Tell this thread to die gracefully. */ public synchronized void destroy() diff --git a/src/org/boncey/lcdjava/util/WidgetUpdater.java b/src/main/java/org/lcdproc/lcdjava/util/WidgetUpdater.java similarity index 90% rename from src/org/boncey/lcdjava/util/WidgetUpdater.java rename to src/main/java/org/lcdproc/lcdjava/util/WidgetUpdater.java index 3271c92..17300ff 100644 --- a/src/org/boncey/lcdjava/util/WidgetUpdater.java +++ b/src/main/java/org/lcdproc/lcdjava/util/WidgetUpdater.java @@ -1,6 +1,6 @@ -package org.boncey.lcdjava.util; +package org.lcdproc.lcdjava.util; -import org.boncey.lcdjava.Widget; +import org.lcdproc.lcdjava.Widget; /** * Interface for holding a Widget that will be updated by the WidgetTimer class. diff --git a/src/org/boncey/lcdjava/util/package.html b/src/main/java/org/lcdproc/lcdjava/util/package.html similarity index 100% rename from src/org/boncey/lcdjava/util/package.html rename to src/main/java/org/lcdproc/lcdjava/util/package.html diff --git a/src/org/boncey/lcdjava/LCDSocketPoller.java b/src/org/boncey/lcdjava/LCDSocketPoller.java deleted file mode 100644 index 517c92a..0000000 --- a/src/org/boncey/lcdjava/LCDSocketPoller.java +++ /dev/null @@ -1,160 +0,0 @@ -package org.boncey.lcdjava; - -import java.io.BufferedReader; -import java.io.IOException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.apache.log4j.Logger; - -/** - * Thread that listens for data on the LCD socket. - *

Copyright (c) 2004-2005 Darren Greaves. - * @author Darren Greaves - * @version $Id: LCDSocketPoller.java,v 1.3 2008-07-06 15:38:34 boncey Exp $ - */ -public class LCDSocketPoller implements Runnable -{ - /** - * Version details. - */ - public static final String CVSID = - "$Id: LCDSocketPoller.java,v 1.3 2008-07-06 15:38:34 boncey Exp $"; - - /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(LCDSocketPoller.class); - - /** - * How often to poll for changes. - */ - private static final int POLL = 100; - - /** - * The Pattern that matches ignore/listen events. - */ - private static final Pattern IGNORE_STATUS = Pattern.compile( - "(ignore|listen)\\s+(\\d+).*"); - - /** - * The Reader to read data from. - */ - private BufferedReader _in; - - /** - * The last line of data received. - *

Will be null if no data was received. - */ - private String _lastLine; - - /** - * Flag that tracks if we are alive or not. - */ - private boolean _alive = true; - - /** - * Flag that tracks if we are polling or not. - */ - private boolean _polling = true; - - /** - * The listener to notify of listen/ignore events. - */ - private LCDListener _listener; - - /** - * Public constructor. - * @param in the BufferedReader that will recieve data from the server. - * @param listener the LCDListener that gets notified of screens being - * listened to or ignored. - */ - public LCDSocketPoller(BufferedReader in, LCDListener listener) - { - _in = in; - _listener = listener; - } - - /** - * Poll the socket every 100 milliseconds. - */ - public void run() - { - while (_alive) - { - synchronized (this) - { - try - { - Thread.sleep(POLL); - if (_in.ready()) - { - _lastLine = _in.readLine(); - Matcher m = IGNORE_STATUS.matcher(_lastLine); - if (_lastLine.startsWith(LCD.RESPONSE_ERROR)) - { - _log.warn("Got a response of " + _lastLine + - " from server"); - } - else if (m.matches()) - { - if (_listener != null) - { - boolean listen = (LCD.RESPONSE_LISTEN.equals( - m.group(1))); - try - { - int screenId = Integer.parseInt(m.group(2)); - _listener.setListenStatus(screenId, listen); - } - catch (NumberFormatException e) - { - // Ignore - } - } - } - } - } - catch (InterruptedException e) - { - // Do nothing - } - catch (IOException e) - { - _log.error("Caught IOException", e); - } - } - } - - _polling = false; - } - - /** - * Get the last line received non-blocking. - *

Calling this clears the last line received. - * @return the last line received. - */ - public synchronized String getLastLine() - { - String ret = _lastLine; - _lastLine = null; - return ret; - } - - /** - * Tell this thread to die gracefully. - */ - public void shutdown() - { - _alive = false; - } - - /** - * Is the server still polling for data. - *

Call this after shutdown to see if we are still polling. - * @return true if still polling, false otherwise. - */ - public boolean isPolling() - { - return _polling; - } -} diff --git a/src/org/boncey/lcdjava/demo/Demo.java b/src/org/boncey/lcdjava/demo/Demo.java deleted file mode 100644 index 42d9759..0000000 --- a/src/org/boncey/lcdjava/demo/Demo.java +++ /dev/null @@ -1,101 +0,0 @@ -package org.boncey.lcdjava.demo; - -import org.apache.log4j.Appender; -import org.apache.log4j.BasicConfigurator; -import org.apache.log4j.ConsoleAppender; -import org.apache.log4j.Logger; -import org.apache.log4j.PatternLayout; -import org.boncey.lcdjava.LCD; -import org.boncey.lcdjava.LCDException; -import org.boncey.lcdjava.Screen; -import org.boncey.lcdjava.StringWidget; -import org.boncey.lcdjava.TitleWidget; - -/** - * Sample class to demonstrate how to display various widgets. - * @author Darren Greaves - * @version $Id: Demo.java,v 1.1 2008-07-06 15:38:34 boncey Exp $ - */ -public class Demo -{ - /** - * Version details. - */ - public static final String CVSID = - "$Id: Demo.java,v 1.1 2008-07-06 15:38:34 boncey Exp $"; - - /** - * Logger for log4j. - */ - private static Logger _log = null; - - /** - * Public constructor. - * @param host the LCDd host. - * @param port the LCDd port. - * @throws LCDException if there was a problem connecting to the server. - */ - private Demo(String host, int port) - throws LCDException - { - LCD lcd = new LCD(host, port); - System.out.println("Connected to LCDd: " + lcd); - - // Construct an unactivated Screen - Screen demoScreen = lcd.constructScreen("demo"); - - // Set to foreground priority (still won't be seen until activated) - demoScreen.setPriority(Screen.PRIORITY_FOREGROUND); - - // Add some Widgets to the screen - nothing will be seen until activated - TitleWidget titleWidget = - TitleWidget.construct(demoScreen, "Demo Title"); - StringWidget stringWidget = - StringWidget.construct(demoScreen, 1, 2, "Demo Text"); - - // Must call Screen.activate before activating any of its Widgets - demoScreen.activate(); - - // This will fail (check return value) if Screen not activated - titleWidget.activate(); - stringWidget.activate(); - } - - public static void main(String args[]) - { - if (args.length != 2) - { - System.err.println( - "Usage: java org.boncey.lcdjava.demo.Demo "); - System.exit(-1); - } - - configureLogging(); - - String host = args[0]; - int port = Integer.parseInt(args[1]); - - try - { - Demo demo = new Demo(host, port); - } - catch (LCDException e) - { - e.printStackTrace(); - } - } - - /** - * Configure log4j before we use the logger in this class. - */ - private static void configureLogging() - { - PatternLayout layout = new PatternLayout( - "%m%n"); - Appender app = new ConsoleAppender(layout); - BasicConfigurator.configure(app); - - _log = Logger.getLogger(Demo.class); - } -} - diff --git a/src/org/boncey/lcdjava/idler/RSSIdler.java b/src/org/boncey/lcdjava/idler/RSSIdler.java deleted file mode 100644 index 338ef85..0000000 --- a/src/org/boncey/lcdjava/idler/RSSIdler.java +++ /dev/null @@ -1,316 +0,0 @@ -package org.boncey.lcdjava.idler; - -import com.sun.syndication.feed.synd.SyndEntry; -import com.sun.syndication.feed.synd.SyndFeed; -import com.sun.syndication.feed.WireFeed; -import com.sun.syndication.fetcher.FeedFetcher; -import com.sun.syndication.fetcher.FetcherException; -import com.sun.syndication.fetcher.impl.FeedFetcherCache; -import com.sun.syndication.fetcher.impl.HashMapFeedInfoCache; -import com.sun.syndication.fetcher.impl.HttpURLFeedFetcher; -import com.sun.syndication.fetcher.impl.SyndFeedInfo; -import com.sun.syndication.io.FeedException; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Calendar; -import java.util.Date; -import java.util.Iterator; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.apache.log4j.Logger; -import org.boncey.lcdjava.LCDException; -import org.w3c.dom.Element; - -/** - * Display the titles of an RSS feed. - *

Copyright (c) 2004-2005 Darren Greaves. - * @author Darren Greaves - * @version $Id: RSSIdler.java,v 1.2 2005-03-03 14:13:16 boncey Exp $ - */ -public class RSSIdler extends AbstractIdler -{ - /** - * Version details. - */ - public static final String CVSID = - "$Id: RSSIdler.java,v 1.2 2005-03-03 14:13:16 boncey Exp $"; - - /** - * Logger for log4j. - */ - private static Logger _log = Logger.getLogger(RSSIdler.class); - - /** - * Pad the display to enhance readability. - */ - private static final String PADDING = " *** "; - - /** - * The User Agent for fetching feeds. - */ - private static final String USER_AGENT = - "FeedFetcher (Rome Client)/0.1"; - - /** - * The text to display. - */ - private String _text = ""; - - /** - * The feed URL. - */ - private URL _feed; - - /** - * Does this Idler have anything to display? - */ - private boolean _valid; - - /** - * How often to update this feed in minutes. - */ - private int _update; - - /** - * Limit the number of entries to this number. - * Will only show the first 'n' entries if set, otherwise shows all. - */ - private int _limit = -1; - - /** - * How many minutes old this feed has to be before we stop displaying it? - *

The feed will continue to be checked in the background so will - * re-appear when it has been updated. - */ - private int _expires = -1; - - /** - * Whether or not to display the feed's description field. - *

False unless specified as includeDescription="true" in - * the config. - */ - private boolean _includeDescription; - - /** - * Any entry matching this pattern will be ignored. - *

Example: ignoreEntryPattern=".*advert.*" to skip ads. - */ - private Pattern _ignoreEntryPattern; - - /** - * Public constructor, read the text from the config. - * @param config the XML element for this Idler. - * @param name the name of the Idler. - * @throws LCDException if there was a problem. - */ - public RSSIdler(Element config, String name) - throws LCDException - { - super(config, name); - - try - { - String feed = config.getAttribute("feed"); - String update = config.getAttribute("update"); - String limit = config.getAttribute("limit"); - String expires = config.getAttribute("expires"); - String ignoreEntryPattern = - config.getAttribute("ignoreEntryPattern"); - String includeDescription = - config.getAttribute("includeDescription"); - - _includeDescription = - Boolean.valueOf(includeDescription).booleanValue(); - _update = Integer.parseInt(update) * - IdlerLoader.ONE_SECOND * - IdlerLoader.ONE_MINUTE; - if (expires.length() > 0) - { - _expires = Integer.parseInt(expires); - } - if (limit.length() > 0) - { - _limit = Integer.parseInt(limit); - } - - _feed = new URL(feed); - _ignoreEntryPattern = Pattern.compile(ignoreEntryPattern); - - RSSUpdater updater = new RSSUpdater(); - Thread thread = new Thread(updater); - thread.start(); - - } - catch (MalformedURLException e) - { - throw new LCDException(e); - } - } - - /** - * Re-read the feed from the given URL. - * @return true if success, false otherwise. - */ - private boolean updateFeed() - { - _log.info("Fetching " + _feed); - - StringBuffer text = new StringBuffer(); - boolean success = false; - boolean expired = false; - try - { - FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); - FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache); - feedFetcher.setUserAgent(USER_AGENT); - SyndFeed feed = feedFetcher.retrieveFeed(_feed); - WireFeed wire = feed.createWireFeed(); - SyndFeedInfo info = feedInfoCache.getFeedInfo(_feed); - long lastModified = - Long.parseLong(String.valueOf(info.getLastModified())); - Date pubDate = new Date(lastModified); - - int max; - if (_limit > 0) - { - max = _limit; - } - else - { - max = feed.getEntries().size(); - } - int i = 0; - - if (_includeDescription) - { - text.append(PADDING + feed.getDescription()); - } - - for (Iterator it = feed.getEntries().iterator(); - it.hasNext() && i < max;) - { - SyndEntry entry = (SyndEntry)it.next(); - String title = entry.getTitle(); - Matcher m = _ignoreEntryPattern.matcher(title); - if (!m.matches()) - { - text.append(PADDING + title); - i++; - } - } - - synchronized (_text) - { - _text = text.toString(); - } - - if (pubDate != null && _expires != -1) - { - Calendar feedCal = Calendar.getInstance(); - feedCal.setTime(pubDate); - - feedCal.add(Calendar.MINUTE, _expires); - - if (feedCal.getTime().before(new Date())) - { - expired = true; - } - } - - if (_text.length() > 0 && !expired) - { - success = true; - } - } - catch (FetcherException e) - { - // HTTP error, don't abort - temporary error. - _log.warn("Failed fetching feed " + _feed + " with status code " + - e.getResponseCode()); - } - catch (FeedException e) - { - // Feed error (likely invalid XML), don't abort - temporary error. - _log.warn("Failed parsing feed " + _feed + " with message " + e); - } - catch (IOException e) - { - // Network error, don't abort - temporary error. - _log.warn("Failed fetching feed " + _feed, e); - } - - return success; - } - - /** - * Is there any text to display? - * @return true if there is anything to display, - * false otherwise. - */ - public boolean isValid() - { - return _valid; - } - - /** - * Get the text to display. - * @return the text to display. - */ - public synchronized String getDisplayText() - { - return _text; - } - - /** - * Idler is being destroyed, do nothing. - */ - public void destroy() - { - } - - /** - * Return a String representing this object. - * @return a String representing this object. - */ - public String toString() - { - return "Feed = " + _feed + "; Valid = " + _valid; - } - - /** - * Thread for handling periodic updates. - * @author Darren Greaves - */ - private class RSSUpdater implements Runnable - { - /** - * Public constructor. - */ - public RSSUpdater() - { - } - - /** - * Update the feed. - */ - public void run() - { - while (true) - { - synchronized (this) - { - try - { - _valid = updateFeed(); - Thread.sleep(_update); - } - catch (InterruptedException e) - { - // Do nothing - } - } - } - } - } -}