001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029: package com.jeta.open.i18n;
030:
031: import java.awt.ComponentOrientation;
032: import java.awt.Insets;
033: import java.util.Iterator;
034: import java.util.LinkedList;
035: import java.util.List;
036:
037: import com.jeta.forms.components.panel.FormPanel;
038: import com.jeta.forms.gui.form.FormComponent;
039: import com.jeta.forms.gui.form.GridComponent;
040: import com.jeta.forms.gui.form.GridView;
041: import com.jgoodies.forms.layout.CellConstraints;
042: import com.jgoodies.forms.layout.ColumnSpec;
043: import com.jgoodies.forms.layout.FormLayout;
044: import com.jgoodies.forms.layout.RowSpec;
045: import com.jgoodies.forms.layout.FormSpec.DefaultAlignment;
046:
047: /**
048: * Provides some convenience behavior for flipping sides in column
049: * specifications, arrays of column specifications and encoded column specs.
050: *
051: * Credit to Karsten Lentzsch of JGoodies for the foundation code for this
052: * utility class. For further information see his example entitled <a
053: * href="http://www.java2java.com/Code/Java/Swing-Components/Buildpanelscomponentorientationlefttorightvsrighttoleft.htm">
054: * "Build panels component orientation: left-to-right vs. right-to-left"</a>
055: *
056: * @author Todd Viegut
057: * @since Abeille 2.1
058: * @version 1.0, 06.24.2007
059: */
060: public class I18NUtils {
061: public static FormPanel applyComponentOrientation(FormPanel form,
062: ComponentOrientation orientation) {
063: if (!orientation.isLeftToRight()) {
064: flip((GridView) form.getFormAccessor());
065: }
066: return form;
067: }
068:
069: /**
070: * Convenience method which
071: *
072: * @param view
073: * The Abeille <code>GridView</code> instance upon which to
074: * interrogate and re-orient row and columns specs.
075: */
076: private static void flip(GridView view) {
077: FormLayout layout = view.getFormLayout();
078: //
079: int columnCount = layout.getColumnCount();
080: int rowCount = layout.getRowCount();
081: ColumnSpec[] colSpecs = new ColumnSpec[columnCount];
082: RowSpec[] rowSpecs = new RowSpec[rowCount];
083: //
084: List views = new LinkedList();
085: GridComponent[][] components = new GridComponent[columnCount][rowCount];
086: for (int column = 1; column <= columnCount; column++) {
087: colSpecs[column - 1] = layout.getColumnSpec(column);
088:
089: for (int row = 1; row <= rowCount; row++) {
090: rowSpecs[row - 1] = layout.getRowSpec(row);
091:
092: GridComponent gc = view.getGridComponent(column, row);
093: components[column - 1][row - 1] = gc;
094: if ((gc != null) && (gc instanceof FormComponent)) {
095: views.add((GridView) gc.getBeanDelegate());
096: }
097: }
098: }
099:
100: ColumnSpec[] flippedColSpecs = flipped(colSpecs);
101: for (int column = 1; column <= columnCount; column++) {
102: layout.setColumnSpec(column, flippedColSpecs[column - 1]);
103: }
104:
105: for (Iterator i = views.iterator(); i.hasNext();) {
106: GridView gridView = (GridView) i.next();
107: flip(gridView);
108: }
109:
110: for (int row = 1; row <= rowCount; row++) {
111: for (int column = 1; column <= columnCount; column++) {
112: GridComponent component = components[column - 1][row - 1];
113: if (component != null) {
114: try {
115: CellConstraints cc = view
116: .getConstraints(component);
117: if (cc != null) {
118: CellConstraints flippedCC = flipHorizontally(
119: cc, columnCount);
120: view.setConstraints(component, flippedCC);
121: }
122: } catch (Exception e) {
123: }
124: }
125: }
126: }
127: }
128:
129: /**
130: * Flips the default alignment of the given column specification and returns
131: * a new column specification object with the flipped alignment and the same
132: * size and growing behavior as the original.
133: *
134: * @param spec
135: * the original column specification
136: * @return the column specification with flipped default alignment
137: */
138: private static ColumnSpec flipped(ColumnSpec spec) {
139: DefaultAlignment alignment = spec.getDefaultAlignment();
140: if (alignment == ColumnSpec.LEFT)
141: alignment = ColumnSpec.RIGHT;
142: else if (alignment == ColumnSpec.RIGHT)
143: alignment = ColumnSpec.LEFT;
144: return new ColumnSpec(alignment, spec.getSize(), spec
145: .getResizeWeight());
146: }
147:
148: /**
149: * Returns an array of column specifications that is built from the given
150: * array by flipping each column spec and reversing their order.
151: *
152: * @param original
153: * the original array of column specifications
154: * @return an array of flipped column specs in reversed order
155: */
156: private static ColumnSpec[] flipped(ColumnSpec[] original) {
157: int length = original.length;
158: ColumnSpec[] flipped = new ColumnSpec[length];
159: for (int i = 0; i < length; i++) {
160: flipped[i] = flipped(original[length - 1 - i]);
161: }
162: return flipped;
163: }
164:
165: /**
166: * Returns an array of column specifications that is built from the given
167: * encoded column specifications by flipping each column spec and reversing
168: * their order.
169: *
170: * @param encodedColumnSpecs
171: * the original comma-separated encoded column specifications
172: * @return an array of flipped column specs in reversed order
173: */
174: private static ColumnSpec[] flipped(String encodedColumnSpecs) {
175: return flipped(ColumnSpec.decodeSpecs(encodedColumnSpecs));
176: }
177:
178: /**
179: * Creates and returns a horizontally flipped clone of the given cell
180: * constraints object. Flips the horizontal alignment and the left and right
181: * insets.
182: *
183: * @param cc
184: * the original cell constraints object
185: * @return the flipped cell constraints with flipped horizontal alignment,
186: * and flipped left and right insets - if any
187: */
188: private static CellConstraints flipHorizontally(CellConstraints cc) {
189: CellConstraints.Alignment flippedHAlign = cc.hAlign;
190: if (flippedHAlign == CellConstraints.LEFT)
191: flippedHAlign = CellConstraints.RIGHT;
192: else if (flippedHAlign == CellConstraints.RIGHT)
193: flippedHAlign = CellConstraints.LEFT;
194:
195: CellConstraints flipped = new CellConstraints(cc.gridX,
196: cc.gridY, cc.gridWidth, cc.gridHeight, flippedHAlign,
197: cc.vAlign);
198: if (cc.insets != null) {
199: flipped.insets = new Insets(cc.insets.top, cc.insets.right,
200: cc.insets.bottom, cc.insets.left);
201: }
202: return flipped;
203: }
204:
205: /**
206: * Creates and returns a horizontally flipped clone of the given cell
207: * constraints object with the grid position adjusted to the given column
208: * count. Flips the horizontal alignment and the left and right insets. And
209: * swaps the left and right cell positions according to the specified column
210: * count.
211: *
212: * @param cc
213: * the original cell constraints object
214: * @param columnCount
215: * the number of columns; used to swap the left and right cell
216: * bounds
217: * @return the flipped cell constraints with flipped horizontal alignment,
218: * and flipped left and right insets - if any
219: */
220: private static CellConstraints flipHorizontally(CellConstraints cc,
221: int columnCount) {
222: CellConstraints flipped = flipHorizontally(cc);
223: flipped.gridX = columnCount + 1 - cc.gridX
224: - (flipped.gridWidth - 1);
225: return flipped;
226: }
227: }
|