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 | */
|
---|
16 | package org.tp23.antinstaller.renderer.swing;
|
---|
17 |
|
---|
18 | import java.awt.event.ItemEvent;
|
---|
19 | import java.awt.event.ItemListener;
|
---|
20 | import java.util.ResourceBundle;
|
---|
21 |
|
---|
22 | import javax.swing.JComboBox;
|
---|
23 | import javax.swing.JLabel;
|
---|
24 | import javax.swing.JPanel;
|
---|
25 |
|
---|
26 | import org.tp23.antinstaller.input.LargeSelectInput;
|
---|
27 | import org.tp23.antinstaller.input.OutputField;
|
---|
28 | import org.tp23.gui.GBCF;
|
---|
29 |
|
---|
30 | public class LargeSelectInputRenderer
|
---|
31 | extends SwingOutputFieldRenderer {
|
---|
32 |
|
---|
33 | private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
|
---|
34 |
|
---|
35 | protected LargeSelectInput inputField;
|
---|
36 |
|
---|
37 | protected JLabel fieldLabel = new AILabel();
|
---|
38 | protected JComboBox optionCombo = new JComboBox();
|
---|
39 |
|
---|
40 | //private int numOfEntries = 2;
|
---|
41 |
|
---|
42 | public LargeSelectInputRenderer() {
|
---|
43 | }
|
---|
44 | public void initComponent(JPanel parent){
|
---|
45 | try {
|
---|
46 | jbInit();
|
---|
47 | }
|
---|
48 | catch(Exception e) {
|
---|
49 | e.printStackTrace();
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | public void setOutputField(OutputField inputField) {
|
---|
56 | this.inputField=(LargeSelectInput)inputField;
|
---|
57 | //this.numOfEntries=this.inputField.getOptions().length;
|
---|
58 | }
|
---|
59 | public void updateInputField(){
|
---|
60 |
|
---|
61 | int selectedIdx = optionCombo.getSelectedIndex();
|
---|
62 | if (selectedIdx != -1) {
|
---|
63 | inputField.setValue(inputField.getOptions()[selectedIdx].value);
|
---|
64 |
|
---|
65 | }else{
|
---|
66 | inputField.setValue(inputField.getDefaultValue());
|
---|
67 | }
|
---|
68 | }
|
---|
69 | public void updateDefaultValue(){
|
---|
70 | if(!inputField.isEditted()){
|
---|
71 |
|
---|
72 | String newDefault = inputField.getDefaultValue();
|
---|
73 |
|
---|
74 | for(int i=0;i<optionCombo.getItemCount();i++){
|
---|
75 | if(newDefault.equals(inputField.getOptions()[i].value)){
|
---|
76 | optionCombo.setSelectedIndex(i);
|
---|
77 | break;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | private void jbInit() throws Exception {
|
---|
84 | fieldLabel.setText(inputField.getDisplayText());
|
---|
85 | LargeSelectInput.Option[] options = inputField.getOptions();
|
---|
86 |
|
---|
87 |
|
---|
88 | for (int i = 0; i < options.length; i++) {
|
---|
89 | optionCombo.addItem(options[i].getText());
|
---|
90 | if(options[i].value.equals(inputField.getDefaultValue())){
|
---|
91 | optionCombo.setSelectedIndex(i);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | optionCombo.addItemListener(new ItemListener(){
|
---|
95 | public void itemStateChanged(ItemEvent e) {
|
---|
96 | inputField.setEditted(true);
|
---|
97 | }
|
---|
98 | });
|
---|
99 | }
|
---|
100 | public int addSelf(JPanel content,GBCF cf, int row,boolean overflow) {
|
---|
101 | content.add(fieldLabel,cf.getCell(row,0));
|
---|
102 | content.add(optionCombo,cf.getCell(row,1));
|
---|
103 | if(overflow){
|
---|
104 | optionCombo.setPreferredSize(SizeConstants.OVERFLOW_FIELD_SIZE);
|
---|
105 | }
|
---|
106 | return ++row;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * renderError
|
---|
111 | */
|
---|
112 | public void renderError() {
|
---|
113 | ctx.getMessageRenderer().printMessage(res.getString("notValidSelection"));
|
---|
114 | optionCombo.requestFocus();
|
---|
115 | }
|
---|
116 | }
|
---|