source: release-kits/lirk3/bin/ant-installer/src/org/tp23/antinstaller/renderer/swing/SelectInputRenderer.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 4.0 KB
Line 
1/*
2 * Copyright 2005 Paul Hinds
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.renderer.swing;
17
18import java.awt.event.ItemEvent;
19import java.awt.event.ItemListener;
20import java.util.Enumeration;
21import java.util.ResourceBundle;
22
23import javax.swing.ButtonGroup;
24import javax.swing.JLabel;
25import javax.swing.JPanel;
26import javax.swing.JRadioButton;
27
28import org.tp23.antinstaller.input.OutputField;
29import org.tp23.antinstaller.input.SelectInput;
30import org.tp23.gui.GBCF;
31
32public class SelectInputRenderer
33 extends SwingOutputFieldRenderer {
34
35 private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
36
37 protected SelectInput inputField;
38
39 protected JLabel fieldLabel = new AILabel();
40 protected ButtonGroup optionGroup = new ButtonGroup();
41
42 public SelectInputRenderer() {
43 }
44 public void initComponent(JPanel parent){
45 try {
46 jbInit();
47 }
48 catch(Exception e) {
49 ctx.log(e.getMessage());
50 if(ctx.getInstaller().isVerbose()) {
51 ctx.log(e);
52 }
53 }
54 }
55
56
57
58 public void setOutputField(OutputField inputField) {
59 this.inputField = (SelectInput)inputField;
60 }
61 public void updateInputField(){
62 Enumeration enumumeration = optionGroup.getElements();
63 int i = 0;
64 for(; enumumeration.hasMoreElements(); i++){
65 JRadioButton o = (JRadioButton)enumumeration.nextElement();
66 if(o.isSelected()) {
67 inputField.setValue(inputField.getOptions()[i].value);
68 break;
69 }
70 }
71 if(i > inputField.getOptions().length) {
72 inputField.setValue(inputField.getDefaultValue());
73 }
74 }
75 public void updateDefaultValue() {
76 if(!inputField.isEditted()) {
77 String newDefault = inputField.getDefaultValue();
78 Enumeration enumeration = optionGroup.getElements();
79 for(int i = 0; enumeration.hasMoreElements(); i++){
80 if(newDefault.equals(inputField.getOptions()[i].value)) {
81 JRadioButton jrb = (JRadioButton) enumeration.nextElement();
82 jrb.setSelected(true);
83 // @todo should break?
84 } else {
85 enumeration.nextElement();
86 }
87 }
88 }
89 }
90
91 private void jbInit() throws Exception {
92 fieldLabel.setText(inputField.getDisplayText());
93 SelectInput.Option[] options = inputField.getOptions();
94
95 for (int i = 0; i < options.length; i++) {
96 JRadioButton jrb = new AIRadioButton(options[i].getText());
97 optionGroup.add(jrb);
98 if(options[i].value.equals(inputField.getDefaultValue())){
99 jrb.setSelected(true);
100 }
101 jrb.addItemListener(new ItemListener(){
102 public void itemStateChanged(ItemEvent e) {
103 inputField.setEditted(true);
104 }
105 });
106 }
107 }
108 public int addSelf(JPanel content,GBCF cf, int row,boolean overflow) {
109 content.add(fieldLabel,cf.getCell(row, 0));
110 Enumeration enumeration = optionGroup.getElements();
111 // there should be at least two
112 enumeration.hasMoreElements();
113 AIRadioButton jrb = (AIRadioButton)enumeration.nextElement();
114 content.add(jrb,cf.getCell(row++, 1));
115 if(overflow) {
116 jrb.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
117 }
118 JPanel empty = new JPanel();
119 while(enumeration.hasMoreElements()){
120 jrb = (AIRadioButton)enumeration.nextElement();
121 content.add(empty,cf.getCell(row, 0));
122 content.add(jrb,cf.getCell(row++, 1));
123 if(overflow) {
124 jrb.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
125 }
126 }
127 return row;
128 }
129
130 /**
131 * renderError
132 */
133 public void renderError() {
134 ctx.getMessageRenderer().printMessage(res.getString("notValidSelection"));
135 // fixed BUG:1295944 ctx.getMessageRenderer().printMessage("Not a valid selection");
136 //optionGroup.requestFocus();
137 }
138}
Note: See TracBrowser for help on using the repository browser.