001: package org.enhydra.jawe.base.panel.panels;
002:
003: import java.awt.Color;
004: import java.awt.Component;
005: import java.awt.Dimension;
006: import java.awt.event.KeyAdapter;
007: import java.awt.event.KeyEvent;
008:
009: import javax.swing.Box;
010: import javax.swing.BoxLayout;
011: import javax.swing.JLabel;
012: import javax.swing.JPanel;
013: import javax.swing.JScrollPane;
014: import javax.swing.JTextArea;
015: import javax.swing.SwingConstants;
016:
017: import org.enhydra.jawe.ResourceManager;
018: import org.enhydra.jawe.Settings;
019: import org.enhydra.jawe.base.panel.PanelContainer;
020: import org.enhydra.jawe.base.panel.PanelSettings;
021: import org.enhydra.shark.xpdl.XMLElement;
022:
023: /**
024: * Creates panel with JLabel and JTextArea.
025: * @author Sasa Bojanic
026: */
027: public class XMLMultiLineTextPanel extends XMLBasicPanel {
028:
029: public static int SIZE_SMALL = 0;
030: public static int SIZE_MEDIUM = 1;
031: public static int SIZE_LARGE = 2;
032: public static int SIZE_EXTRA_LARGE = 3;
033:
034: public static Dimension textAreaDimensionSmall = new Dimension(200,
035: 60);
036: public static Dimension textAreaDimensionMedium = new Dimension(
037: 300, 90);
038: public static Dimension textAreaDimensionLarge = new Dimension(400,
039: 120);
040: public static Dimension textAreaDimensionExtraLarge = new Dimension(
041: 400, 300);
042:
043: protected JTextArea jta;
044: protected JLabel jl;
045:
046: protected boolean falseRequiredForCC = false;
047:
048: public XMLMultiLineTextPanel(PanelContainer pc, XMLElement myOwner,
049: boolean isVertical, int type, boolean wrapLines,
050: boolean isEnabled) {
051: this (pc, myOwner, myOwner.toName(), myOwner.isRequired(),
052: isVertical, type, wrapLines, isEnabled);
053: }
054:
055: public XMLMultiLineTextPanel(PanelContainer pc, XMLElement myOwner,
056: String labelKey, boolean isFalseRequired,
057: boolean isVertical, int type, boolean wrapLines,
058: boolean isEnabled) {
059:
060: super (pc, myOwner, "", false, false, true);
061:
062: this .falseRequiredForCC = isFalseRequired;
063:
064: boolean rightAllignment = false;
065:
066: Color bkgCol = new Color(245, 245, 245);
067: if (pc != null) {
068: Settings settings = pc.getSettings();
069:
070: rightAllignment = settings
071: .getSettingBoolean("XMLBasicPanel.RightAllignment");
072:
073: if (settings instanceof PanelSettings) {
074: bkgCol = ((PanelSettings) settings)
075: .getBackgroundColor();
076: }
077:
078: }
079:
080: JScrollPane jsp = new JScrollPane();
081: jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
082: jsp.setAlignmentY(Component.TOP_ALIGNMENT);
083:
084: String lbl = "";
085: if (pc != null) {
086: lbl = pc.getSettings().getLanguageDependentString(
087: labelKey + "Key")
088: + ": ";
089: } else {
090: lbl = ResourceManager.getLanguageDependentString(labelKey
091: + "Key")
092: + ": ";
093: }
094: jl = new JLabel(lbl);
095: jl.setAlignmentX(Component.LEFT_ALIGNMENT);
096: jl.setAlignmentY(Component.TOP_ALIGNMENT);
097:
098: if (rightAllignment) {
099: jl.setHorizontalAlignment(SwingConstants.RIGHT);
100: } else {
101: jl.setHorizontalAlignment(SwingConstants.LEFT);
102: }
103:
104: jta = new JTextArea();
105:
106: jta.setTabSize(4);
107: jta.setText(myOwner.toValue());
108: jta.getCaret().setDot(0);
109: jta.setLineWrap(wrapLines);
110: jta.setWrapStyleWord(wrapLines);
111:
112: jta.setAlignmentX(Component.LEFT_ALIGNMENT);
113: jta.setAlignmentY(Component.TOP_ALIGNMENT);
114:
115: jta.setEnabled(isEnabled);
116: jta.setBackground(bkgCol);
117:
118: final XMLPanel p = this ;
119: jta.addKeyListener(new KeyAdapter() {
120: public void keyPressed(KeyEvent e) {
121: if (getPanelContainer() == null)
122: return;
123: if (PanelUtilities.isModifyingEvent(e)) {
124: getPanelContainer().panelChanged(p, e);
125: }
126: }
127: });
128:
129: jsp.setViewportView(jta);
130: jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
131: jsp.setAlignmentY(Component.TOP_ALIGNMENT);
132: Dimension dim = new Dimension(textAreaDimensionMedium);
133: if (type == XMLMultiLineTextPanel.SIZE_SMALL) {
134: dim = new Dimension(textAreaDimensionSmall);
135: } else if (type == XMLMultiLineTextPanel.SIZE_MEDIUM) {
136: dim = new Dimension(textAreaDimensionMedium);
137: } else if (type == XMLMultiLineTextPanel.SIZE_LARGE) {
138: dim = new Dimension(textAreaDimensionLarge);
139: } else {
140: dim = new Dimension(textAreaDimensionExtraLarge);
141: }
142: jsp.setPreferredSize(dim);
143:
144: JPanel mainPanel = this ;
145: if (isVertical) {
146: mainPanel = new JPanel();
147: mainPanel.setLayout(new BoxLayout(mainPanel,
148: BoxLayout.Y_AXIS));
149: }
150: if (rightAllignment && !isVertical) {
151: mainPanel.add(Box.createHorizontalGlue());
152: }
153: mainPanel.add(jl);
154: if (!rightAllignment && !isVertical) {
155: mainPanel.add(Box.createHorizontalGlue());
156:
157: }
158: mainPanel.add(jsp);
159: if (!rightAllignment && !isVertical) {
160: mainPanel.add(Box.createHorizontalGlue());
161: }
162: if (isVertical) {
163: add(mainPanel);
164: }
165:
166: }
167:
168: public boolean validateEntry() {
169: if (isEmpty() && getOwner().isRequired() && falseRequiredForCC
170: && !getOwner().isReadOnly()) {
171: //TODO CHECK THIS
172: XMLBasicPanel.defaultErrorMessage(this .getWindow(), jl
173: .getText());
174: jta.requestFocus();
175: return false;
176: }
177: return true;
178: }
179:
180: public boolean isEmpty() {
181: return getText().trim().equals("");
182: }
183:
184: public void setElements() {
185: if (!getOwner().isReadOnly()) {
186: myOwner.setValue(getText().trim());
187: }
188: }
189:
190: public String getText() {
191: return jta.getText();
192: }
193:
194: public Object getValue() {
195: return getText();
196: }
197:
198: public void requestFocus() {
199: jta.requestFocus();
200: }
201:
202: }
|