Skip to content

Commit

Permalink
Added category to each tool to indicate what kind of tool it is. Ensu…
Browse files Browse the repository at this point in the history
…red the order of tools in the menu is predictable.
  • Loading branch information
jawi committed Sep 9, 2010
1 parent 3828bfc commit 74dd0dc
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 13 deletions.
30 changes: 28 additions & 2 deletions api/src/main/java/nl/lxtreme/ols/api/tools/Tool.java
Expand Up @@ -38,8 +38,34 @@
*/
public interface Tool
{
// INNER TYPES

/**
* Provides a category for pluggable tools.
*/
public static enum Category
{
/** Denotes a tool that decodes something. */
DECODER,
/** Denotes a tool that measures something. */
MEASURE,
/** For tools that neither decode nor measure something. */
OTHER;
}

// METHODS

/**
* Returns the category for this tool.
* <p>
* Each tool <em>must</em> provide a category in order to be grouped
* correctly.
* </p>
*
* @return a category, cannot be <code>null</code>.
*/
Category getCategory();

/**
* Is called to get the name for the menu entry.
* <p>
Expand All @@ -49,7 +75,7 @@ public interface Tool
*
* @return name for this tool
*/
public String getName();
String getName();

/**
* This method is invoked when the tool is selected from the Tools menu. It
Expand All @@ -73,6 +99,6 @@ public interface Tool
* the callback to report the status of the tool to, cannot be
* <code>null</code>.
*/
public void process( final Window aOwner, final DataContainer aData, final ToolContext aContext,
void process( final Window aOwner, final DataContainer aData, final ToolContext aContext,
final AnalysisCallback aCallback );
}
35 changes: 33 additions & 2 deletions client/src/main/java/nl/lxtreme/ols/client/MainFrame.java
Expand Up @@ -230,8 +230,11 @@ public final void addToolMenuItem( final String aToolName )
this.toolsMenu.remove( this.noToolsItem );

final JMenuItem menuItem = createToolMenuItem( aToolName );
// Determine where in the menu we should add the menu item, this way, we
// can make the menu appear consistent...
final int idx = determineToolMenuItemIndex( menuItem );

this.toolsMenu.add( menuItem );
this.toolsMenu.add( menuItem, idx );

updateToolMenuState( aToolName, menuItem, true /* aAdded */);
}
Expand Down Expand Up @@ -547,11 +550,39 @@ private JMenuItem createToolMenuItem( final String aToolName )
*/
private int determineDeviceMenuItemIndex( final JMenuItem aMenuItem )
{
final String newMenuItem = aMenuItem.getText();

int idx = -1;
for ( int i = 0; ( idx < 0 ) && ( i < this.deviceMenu.getItemCount() ); i++ )
{
final String nameA = this.deviceMenu.getItem( i ).getText();
final int comparison = aMenuItem.getText().compareTo( nameA );
final int comparison = newMenuItem.compareTo( nameA );
if ( comparison < 0 )
{
idx = i;
}
}
return idx;
}

/**
* Determines the index in the menu where the given menu item should be
* inserted.
*
* @param aMenuItem
* the menu item to add, cannot be <code>null</code>.
* @return the position in the menu to add the given menu item, -1 if the menu
* item should be added as last item.
*/
private int determineToolMenuItemIndex( final JMenuItem aMenuItem )
{
final String newMenuItem = aMenuItem.getText();

int idx = -1;
for ( int i = 0; ( idx < 0 ) && ( i < this.toolsMenu.getItemCount() ); i++ )
{
final String nameA = this.toolsMenu.getItem( i ).getText();
final int comparison = newMenuItem.compareTo( nameA );
if ( comparison < 0 )
{
idx = i;
Expand Down
Expand Up @@ -196,14 +196,16 @@ else if ( PROPERTY_STATE.equals( name ) )
// CONSTRUCTORS

/**
* Creates a new BaseTool instance.
* Creates a new BaseAsyncTool instance.
*
* @param aCategory
* the category of this tool;
* @param aName
* the name of the tool as it should appear in the main UI.
*/
protected BaseAsyncTool( final String aName )
protected BaseAsyncTool( final Category aCategory, final String aName )
{
super( aName );
super( aCategory, aName );
}

// METHODS
Expand Down
24 changes: 23 additions & 1 deletion tool.base/src/main/java/nl/lxtreme/ols/tool/base/BaseTool.java
Expand Up @@ -39,18 +39,31 @@ public abstract class BaseTool<DIALOG extends JDialog & ToolDialog> implements T
// VARIABLES

private final String name;
private final Category category;
private DIALOG dialog;

// CONSTRUCTORS

/**
* Creates a new BaseTool instance.
*
* @param aCategory
* the category of this tool;
* @param aName
* the name of this tool (to show in the UI).
*/
public BaseTool( final String aName )
protected BaseTool( final Category aCategory, final String aName )
{
if ( aCategory == null )
{
throw new IllegalArgumentException( "Category cannot be null!" );
}
this.category = aCategory;

if ( ( aName == null ) || aName.trim().isEmpty() )
{
throw new IllegalArgumentException( "Name cannot be null or empty!" );
}
this.name = aName;
}

Expand Down Expand Up @@ -87,6 +100,15 @@ else if ( !this.name.equals( other.name ) )
return true;
}

/**
* @see nl.lxtreme.ols.api.tools.Tool#getCategory()
*/
@Override
public final Category getCategory()
{
return this.category;
}

/**
* @see nl.lxtreme.ols.api.tools.Tool#getName()
*/
Expand Down
Expand Up @@ -41,7 +41,7 @@ public class I2CAnalyser extends BaseAsyncTool<I2CProtocolAnalysisDialog, I2CDat
*/
public I2CAnalyser()
{
super( "I2C protocol analyser ..." );
super( Category.DECODER, "I2C protocol analyser ..." );
}

// METHODS
Expand Down
Expand Up @@ -38,7 +38,7 @@ public class MeasurementTool extends BaseTool<MeasurementDialog>
*/
public MeasurementTool()
{
super( "Measure ..." );
super( Category.MEASURE, "Measure ..." );
}

// METHODS
Expand Down
Expand Up @@ -40,7 +40,7 @@ public class SPIAnalyser extends BaseAsyncTool<SPIProtocolAnalysisDialog, SPIDat
*/
public SPIAnalyser()
{
super( "SPI analyser ..." );
super( Category.DECODER, "SPI analyser ..." );
}

// METHODS
Expand Down
Expand Up @@ -40,7 +40,7 @@ public class StateAnalyser extends BaseAsyncTool<StateAnalysisDialog, CapturedDa
*/
public StateAnalyser()
{
super( "State analyser ..." );
super( Category.OTHER, "State analyser ..." );
}

// METHODS
Expand Down
Expand Up @@ -40,7 +40,7 @@ public class UARTAnalyser extends BaseAsyncTool<UARTProtocolAnalysisDialog, UART
*/
public UARTAnalyser()
{
super( "UART analyser ..." );
super( Category.DECODER, "UART analyser ..." );
}

// METHODS
Expand Down

0 comments on commit 74dd0dc

Please sign in to comment.