📄 resourceproperty.java
字号:
/**************/ label = new JLabel("List of Machines"); label.setFont(font.deriveFont(Font.BOLD, 16)); c.weighty = 1.0; //request any extra vertical space c.insets = new Insets(25,0,0,0); //top padding c.gridy = index++; c.gridx = 1; c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.RELATIVE; charPanel_.add(label, c); // reset the values c.fill = GridBagConstraints.HORIZONTAL; c.gridwidth = GridBagConstraints.REMAINDER; c.weighty = 0.0; c.gridx = 0; /////////////////////////////// // Create a list of machines machineList_ = new JList(machineName_); machineList_.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); machineList_.setSelectedIndex(0); int vert = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED; int horiz = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED; JScrollPane listScrollPane = new JScrollPane(machineList_, vert,horiz); // initially display the first machine ((ResourceMachine) machine_.elementAt(0)).showDialog(machinePanel_); JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScrollPane, machinePanel_); splitPane.setOneTouchExpandable(true); splitPane.setDividerLocation(150); c.gridy = index++; charPanel_.add(splitPane, c); } /** * Creates a slider bar * @param panel a JPanel object * @param c a GridBagConstraints object * @param slider a JSlider object * @pre $none * @post $none */ private void createSlider(JPanel panel, GridBagConstraints c, JSlider slider) { slider.setMajorTickSpacing(1); // increment every 1 point slider.setPaintLabels(true); // display the label for MajorTick slider.setPaintTicks(true); //Create the label table Hashtable labelTable = new Hashtable(); float i = 0; for (int counter = 0; counter < 7; counter++) { labelTable.put( new Integer(counter), new JLabel("" + i) ); i += 0.1; } // have to be hardcoded since after 6, the value will be wrong // in windows labelTable.put( new Integer(7), new JLabel("0.7") ); labelTable.put( new Integer(8), new JLabel("0.8") ); labelTable.put( new Integer(9), new JLabel("0.9") ); labelTable.put( new Integer(10), new JLabel("1.0") ); // add the hashtable into the slider slider.setLabelTable( labelTable ); c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 3; c.gridx = 2; panel.add(slider, c); // reset the values c.gridwidth = 1; c.weightx = 0; c.gridx = 0; } /** * Removes all GUI listeners * @pre $none * @post $none */ private void removeListeners() { // remove all the listeners to prevent memory leakages machineList_.removeListSelectionListener(this); ok_.removeActionListener(this); cancel_.removeActionListener(this); peakSlider_.removeChangeListener(this); offSlider_.removeChangeListener(this); holidaySlider_.removeChangeListener(this); } /** * Saves the current grid resource properties * @return <tt>true</tt> if the values have been saved successfully, * <tt> false</tt> otherwise * @pre $none * @post $none */ private boolean saveValue() { name_ = resText_.getText(); peakLoad_ = peakSlider_.getValue(); offLoad_ = offSlider_.getValue(); holidayLoad_ = holidaySlider_.getValue(); comboPolicy_ = combo_.getSelectedIndex(); String errorMsg = ""; // a flag to denote where the error comes from try { errorMsg = "baud rate"; baudRate_ = checkValue(baudRate_, baudText_, false); errorMsg = "time zone"; timeZone_ = checkValue(timeZone_, timeZoneText_, true); errorMsg = "cost per sec"; price_ = checkValue(price_, priceText_, false); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "Invalid value for " + errorMsg + ".", errorMsg + " value error", JOptionPane.ERROR_MESSAGE); return false; } catch (Exception e) { JOptionPane.showMessageDialog(this, e.getMessage(), errorMsg + " value error", JOptionPane.ERROR_MESSAGE); return false; } int size = machine_.size(); for (int i = 0; i < size; i++) { ( (ResourceMachine) machine_.elementAt(i) ).saveValue(); } return true; } /** * Checks whether the given value is correct or not * @param value the number to be checked * @param text a JTextField object * @param timeZone <tt>true</tt> if it is a time zone, <tt>false</tt> * otherwise * @return the correct number * @throws Exception if the value is not in a valid format * @pre text != null * @post $none */ private double checkValue(double value, JTextField text, boolean timeZone) throws Exception { value = new Double( text.getText() ).doubleValue(); if (value < 0.0 && timeZone == false) { throw new Exception("Invalid for having negative value."); } if (timeZone == true) { // the limit for time zone is GMT + 13:00 and GMT - 12:00 if (value > 13 || value < -12) { throw new Exception("Invalid value for GMT Time Zone.\n" + "The valid limits are GMT + 13:00 and GMT - 12:00"); } } return value; } /** * Resets the current values back to the previous ones * @pre $none * @post $none */ private void resetValue() { double num = peakLoad_ / 10.0; peakLabel_.setText("Peak Load: " + num); peakSlider_.setValue(peakLoad_); num = offLoad_ / 10.0; offLabel_.setText("Off-peak Load: " + num); offSlider_.setValue(offLoad_); num = holidayLoad_ / 10.0; holidayLabel_.setText("Holiday Load: " + num); holidaySlider_.setValue(holidayLoad_); // reseting the text fields based on the order of apperances resText_.setText(name_); baudText_.setText("" + baudRate_); archText_.setText(arch_); osText_.setText(os_); timeZoneText_.setText("" + timeZone_); priceText_.setText("" + price_); combo_.setSelectedIndex(comboPolicy_); // reset the values for each gridlet int size = machine_.size(); for (int i = 0; i < size; i++) { ( (ResourceMachine) machine_.elementAt(i) ).resetValue(); } } /** * Loads a section of grid resource characteristics from XML file * @param nodeList a NodeList object * @pre nodeList != null * @post $none */ private void loadXmlResourceCharacteristics(final NodeList nodeList) { Node node; String name, value; int length = nodeList.getLength(); for (int i = 0; i < length; i++) { node = nodeList.item(i); // only element nodes that need to be take care, // the rests are ignored if (node.getNodeType() != Node.ELEMENT_NODE) { continue; } name = node.getNodeName(); if (name.equals("architecture") == true) { value = node.getFirstChild().getNodeValue(); arch_ = value.trim(); } else if (name.equals("operatingSystem") == true) { value = node.getFirstChild().getNodeValue(); os_ = value.trim(); } else if (name.equals("timeZone") == true) { value = node.getFirstChild().getNodeValue(); // must trim the String otherwise get an exception double num = Double.parseDouble(value.trim()); timeZone_ = num; } else if (name.equals("gridDollarPerApplication") == true) { value = node.getFirstChild().getNodeValue(); // must trim the String otherwise get an exception double num = Double.parseDouble(value.trim()); price_ = num; } else if (name.equals("allocationPolicy") == true) { value = node.getFirstChild().getNodeValue(); loadXmlPolicy( value.trim() ); } else if (name.equals("machineList") == true) { machine_.clear(); machineName_.clear(); loadXmlMachine( node.getChildNodes() ); } } } /** * Loads a section of grid resource scheduling policy from XML file * @param value scheduling policy * @pre value != null * @post $none */ private void loadXmlPolicy(String value) { int i = 0; int length = comboValue_.length; for (i = 0; i < length; i++) { if (comboValue_[i].equals(value) == true) { break; } } if (i == length) { i = 0; } comboPolicy_ = i; } /** * Loads a section of grid resource machine from XML file * @param nodeList a NodeList object * @pre nodeList != null * @post $none */ private void loadXmlMachine(final NodeList nodeList) { final int size = nodeList.getLength(); Node node; for (int j = 0; j < size; j++) { node = nodeList.item(j); if (node.getNodeType() == Node.ELEMENT_NODE) { loadXmlResourceMachine(node, size); } } } /** * Loads a section of grid resource machine size from XML file * @param node a Node object * @param size a Machine size * @pre node != null * @pre size >= 0 * @post $none */ private void loadXmlResourceMachine(final Node node, final int size) { try { String nodeName = node.getNodeName(); if (nodeName.equals("totalMachine") == true) { String value = node.getFirstChild().getNodeValue(); // must trim the String otherwise get an exception int num = Integer.parseInt(value.trim()); int total = (size/2) - 1; // this causing other unknown exception, so becareful if (num < 1 || total != num) { throw new NumberFormatException("<totalMachine> error"); } } else if (nodeName.equals("machine") == true) { loadXmlResourceMachineProperty( node.getChildNodes() ); } } catch (NumberFormatException e) { System.out.println(e.getMessage()); e.printStackTrace(); //throw new Exception("Invalid value in <totalUser>"); } catch (Exception obj) { System.out.println("obj = " + obj.getMessage()); obj.printStackTrace(); } } /** * Loads a section of grid resource machine properties from XML file * @param nodeList a NodeList object * @pre nodeList != null * @post $none */ private void loadXmlResourceMachineProperty(final NodeList nodeList) { Node node; String name, value; int length = nodeList.getLength(); for (int i = 0; i < length; i++) { node = nodeList.item(i); name = node.getNodeName(); if (node.getNodeType() == Node.ELEMENT_NODE && name.equals("id") == true) { value = node.getFirstChild().getNodeValue(); // must trim the String otherwise get an exception int id = Integer.parseInt(value.trim()); ResourceMachine obj = new ResourceMachine(id, false, null); obj.loadXml(nodeList); machine_.add(obj); machineName_.add("Machine " + id); break; } // NOTE: ignoring <totalPE> for this time being } }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -