Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Second command-line tutorial



Overview

With this tutorial, we will learn to:
  • Manage the command-line arguments in a main Application and its Plugins
We will keep the arguments we defined for the application (the sizeX and sizeY arguments to set the width and height of the application window), but we will also add arguments to be able to open an image file at the start of the application.

Specify the xml file to specify the command-line arguments for the Plugin


First we will specify the xml file which will define our two arguments for our plugin:
  • The open argument will specify that we want to open a file
  • The image argument will specify the path of the file
To achieve this, we will use an argument group:
   <arguments>
      <argument key="open" type="empty" />
      <argument key="image" type="url" />
      <argumentGroup key="open">
         <argument key="open" mandatory="true" />
         <argument key="image" mandatory="true" />
      </argumentGroup>
   </arguments>

Use the command-line framework in our Plugin

First we define that we use our xml file in our plugin:
   public class OpenImagePlugin extends AbstractMDIPlugin {

     @Override
     public URL getCommandLineConfiguration() {
       return this.getClass().getResource("commandline.xml");
     }     
   }      
We now will use the arguments in the Plugin:
   public class OpenImagePlugin extends AbstractMDIPlugin {
      
     @Override
     public void handleCommandLineArguments(Map<String, ArgumentGroup> argumentGroups, Map<String, Argument> arguments) {
       if (argumentGroups.containsKey("open")) {
         ArgumentGroup group = argumentGroups.get("open");
         File file = (File) group.getArgument("image").getValue();
         try {
           importImage(file);
         } catch (Exception ex) {
           ex.printStackTrace();
         }
       }
     }      
   }

Usage

We can still start our application with the sizeX and sizeY arguments, such as: Or we can use the sizeX and sizeY arguments, such as:
      java -jar CommandlineTutorial2MDI.jar -sizeX=500 -sizeY=500
But now we can also start the application and open an image file, such with:
      java -jar CommandlineTutorial2MDI.jar -open -image=myImage.jpg

See also


Categories: swing | tutorials

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences