Google Answers Logo
View Question
 
Q: Passing configuration data in JAVA question ( No Answer,   1 Comment )
Question  
Subject: Passing configuration data in JAVA question
Category: Computers > Programming
Asked by: gooseman-ga
List Price: $10.00
Posted: 27 Oct 2002 08:23 PST
Expires: 29 Oct 2002 08:33 PST
Question ID: 90544
I have a main GUI where the user can bring up the option to edit
program preferences - they are quite simple. If the user chooses to do
this, a table comes up with the description, parameter name, default
value and custom value (custom value column is editable). The user can
hit ok and the table frame closes. Once done, the user can hit the
execute button on the GUI, and a new simulation is instatiated that
does the hard work. This is supposed to take in the edited "custom"
values or the default values if nothing has been changed. So my
questions are:

Where to I store Object [][] simulationPrefs - in the initial GUI
class, the prefs class or the simulation class?

What's the best way of passing it to and fro classes - as an object,
or a dataVector (the object is converted to a dataVector, so it can be
displayed in the table class).

How do I deal with the possibility that the user won't change the
preferences (currently, the custom column == default column)

Finally, is the is best way of doing this? Is there a better way that
won't require a massive rewrite? I don't really want to store any data
in files, and it is very important that the description is included in
the Object.

Maybe all I need to do is pass the parameter name column and the
custom value columns to the simulation class - but how do I do that if
the user doesn't enter the preferences frame?

It is also important to make things as easy for any future programmers
to change, which is why maybe having the Object [][] simulationPrefs
in the simulation class may be the clearest way of placing things.
Here is the table class code - I've removed most of the irrelevant
bits from it.

// ...imports

class RunOmcdFrame_Preferences extends JFrame{

  DefaultTableModel simulationTable = new DefaultTableModel() {
    public boolean isCellEditable(int row, int column) {
      if (column == 3) {
        return true;
      }
      return false;
    }
  };
  //... GUI
  private String[] columnNames = {"Preference Description",
                                  "Parameter",
                                  "Default",
                                  "Custom"};

  public RunOmcdFrame_Preferences(int selection) {
    userOption = selection;
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    
    simulationTable.setDataVector(getSimulationPrefs(), columnNames);

    JTable tableS = new JTable(simulationTable);
    JTableHeader headerS = tableS.getTableHeader();
    headerS.setReorderingAllowed(false);

    JTableHeader headerM = tableM.getTableHeader();
    headerS.setReorderingAllowed(false);
    tableS.getColumn("Custom").setCellRenderer(
      new MultiRenderer());
    tableS.getColumn("Custom").setCellEditor(
      new MultiEditor());
    tableS.getColumn("Default").setCellRenderer(
      new MultiRenderer());
    calcColumnWidths(tableS); // Set widths

    // ...more GUI

    jTabbedPane1.setSelectedComponent(jScrollPaneSimulation);
    // ...etc for more tables in tabbed pane
  }

  private Object[][] getSimulationPrefs(){
  Object [][] simulationPrefs = new Object[][]
  {
    {"Parameter A description", "intParamA", 
       new Integer(100), new Integer(100)},
    {"Parameter B description", "comboParamB", 
       new ComboString("true"), new ComboString("true")},
    {"Parameter C description", "floatParamC", 
       new Float (12.4), new Float (12.4)},
    {"Parameter D description", "boolParamD", 
       new Boolean(true), new Boolean(true)}};
    return simulationPrefs;
  }
}

class MultiRenderer extends DefaultTableCellRenderer {
  //...snip
}

class MultiEditor implements TableCellEditor {
  //...snip
}

Request for Question Clarification by secret901-ga on 27 Oct 2002 12:15 PST
Hi gooseman,
Instead of storing the configuration data in a two-dimensional array,
have you considered storing them in a separate class?

Clarification of Question by gooseman-ga on 27 Oct 2002 13:22 PST
Not really. If it is possible, it would have to require no changes to
the

class MultiRenderer extends DefaultTableCellRenderer { 
  //...snip 
} 
 
class MultiEditor implements TableCellEditor { 
  //...snip 
}

bits of code, as they expect different objects in the same column and
deal with them appropriately.

What would the benefits be of using a class? I'm guessing it would be
the same implementation as before, but easier to pass around? Maybe
use a get and set method? I've got three different sets of data, all
formatted the same way. How can I use the class to create all three?

Clarification of Question by gooseman-ga on 28 Oct 2002 06:48 PST
Do you need any more information, or this question unclear/too complex
for price set?
Answer  
There is no answer at this time.

Comments  
Subject: Re: Passing configuration data in JAVA question
From: sinsai-ga on 28 Oct 2002 10:34 PST
 
the best approach is to create a class that contains your information.
 create a class object to retrieve the value you need.  below is an
example code:
[code]
import java.util.Hashtable;

public class StateCode {
  //State code, State full name
  private final static String[][] stateCodes = {
    {"CA", "California"},{"NV", "Nevada"},{"WA", "Washington"},
    {"TX", "Texas"},{"UT", "Utah"},{"MT", "Montana"}
  };
  private Hashtable ht = new Hashtable();   //hold: key, value

  //load hashtable with initial state values
  public StateCode() {
    for(int i=0; i<stateCodes.length; i++) {
      ht.put(stateCodes[i][0], stateCodes[i][1]);
    }
  }
  //get state full name
  public String getStateFullName(String stateCode) {
    return (String)ht.get(stateCode);
  }
  //check if state code exists
  public boolean containsStateCode(String stateCode) {
    return ht.containsKey(stateCode);
  }
  //add new state information
  public void addStateCode(String stateCode, String stateFullName) {
    if(!containsStateCode(stateCode)) {
      ht.put(stateCode, stateFullName);
    }
  }
  //change state information
  public void changeStateFullName(String stateCode, String
newStateFullName) {
    if(containsStateCode(stateCode)) {
      removeStateCode(stateCode);
      addStateCode(stateCode, newStateFullName);
    }
  }
  //remove state information
  public void removeStateCode(String stateCode) {
    if(containsStateCode(stateCode)) {
      ht.remove(stateCode);
    }
  }
}
[\code]

hope this was what you were looking for.

sin sai

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy