001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.propertyeditors.css.model;
042:
043: import java.util.StringTokenizer;
044: import javax.swing.DefaultComboBoxModel;
045:
046: /**
047: *
048: * @author Winston Prakash
049: */
050: public class ClipData extends PropertyData {
051:
052: PropertyWithUnitData topValue = new PropertyWithUnitData();
053: PropertyWithUnitData bottomValue = new PropertyWithUnitData();
054: PropertyWithUnitData leftValue = new PropertyWithUnitData();
055: PropertyWithUnitData rightValue = new PropertyWithUnitData();
056:
057: private boolean hasErrors = false;
058:
059: public void setClip(String cip) {
060: String cipString = cip.substring(cip.indexOf("(") + 1, cip
061: .indexOf(")"));
062: StringTokenizer st = new StringTokenizer(cipString, ",");
063:
064: if (st.hasMoreTokens()) {
065: setTop(st.nextToken());
066: }
067: if (st.hasMoreTokens()) {
068: setRight(st.nextToken());
069: }
070: if (st.hasMoreTokens()) {
071: setBottom(st.nextToken());
072: }
073: if (st.hasMoreTokens()) {
074: setLeft(st.nextToken());
075: }
076: }
077:
078: public void setTop(String clipTopStr) {
079: topValue.setUnit(getUnit(clipTopStr));
080: topValue.setValue(clipTopStr.replaceAll(topValue.getUnit(), "")
081: .trim());
082: }
083:
084: public void setBottom(String clipBottomStr) {
085: bottomValue.setUnit(getUnit(clipBottomStr));
086: bottomValue.setValue(clipBottomStr.replaceAll(
087: bottomValue.getUnit(), "").trim());
088: }
089:
090: public void setLeft(String clipLeftStr) {
091: leftValue.setUnit(getUnit(clipLeftStr));
092: leftValue.setValue(clipLeftStr.replaceAll(leftValue.getUnit(),
093: "").trim());
094: }
095:
096: public void setRight(String clipRightStr) {
097: rightValue.setUnit(getUnit(clipRightStr));
098: rightValue.setValue(clipRightStr.replaceAll(
099: rightValue.getUnit(), "").trim());
100: }
101:
102: private String getUnit(String clipStr) {
103: DefaultComboBoxModel unitList = new ClipModel()
104: .getClipUnitList();
105: for (int i = 0; i < unitList.getSize(); i++) {
106: String unit = (String) unitList.getElementAt(i);
107: if (clipStr.trim().endsWith(unit)) {
108: return unit;
109: }
110: }
111: return "";
112: }
113:
114: public void setTopValue(String top) {
115: topValue.setValue(top);
116: }
117:
118: public void setTopUnit(String topUnit) {
119: topValue.setUnit(topUnit);
120: }
121:
122: public void setBottomValue(String bottom) {
123: bottomValue.setValue(bottom);
124: }
125:
126: public void setBottomUnit(String bottomUnit) {
127: bottomValue.setUnit(bottomUnit);
128: }
129:
130: public void setLeftValue(String left) {
131: leftValue.setValue(left);
132: }
133:
134: public void setLeftUnit(String leftUnit) {
135: leftValue.setUnit(leftUnit);
136: }
137:
138: public void setRightValue(String right) {
139: rightValue.setValue(right);
140: }
141:
142: public void setRightUnit(String rightUnit) {
143: rightValue.setUnit(rightUnit);
144: }
145:
146: public String getTopValue() {
147: return topValue.getValue();
148: }
149:
150: public String getTopUnit() {
151: return topValue.getUnit();
152: }
153:
154: public String getBottomValue() {
155: return bottomValue.getValue();
156: }
157:
158: public String getBottomUnit() {
159: return bottomValue.getUnit();
160: }
161:
162: public String getLeftValue() {
163: return leftValue.getValue();
164: }
165:
166: public String getLeftUnit() {
167: return leftValue.getUnit();
168: }
169:
170: public String getRightValue() {
171: return rightValue.getValue();
172: }
173:
174: public String getRightUnit() {
175: return rightValue.getUnit();
176: }
177:
178: public boolean isTopValueInteger() {
179: return topValue.isValueInteger();
180: }
181:
182: public boolean isBottomValueInteger() {
183: return bottomValue.isValueInteger();
184: }
185:
186: public boolean isLeftValueInteger() {
187: return leftValue.isValueInteger();
188: }
189:
190: public boolean isRightValueInteger() {
191: return rightValue.isValueInteger();
192: }
193:
194: public boolean hasErros() {
195: if (topValue.hasValue() || bottomValue.hasValue()
196: || rightValue.hasValue() || leftValue.hasValue()) {
197: return hasErrors;
198: } else {
199: return false;
200: }
201: }
202:
203: public String toString() {
204: String clipString = "";
205: if (!topValue.toString().equals("")) {
206: clipString += " " + topValue.toString();
207: } else {
208: clipString = "";
209: hasErrors = true;
210: }
211: if (!rightValue.toString().equals("")) {
212: clipString += ", " + rightValue.toString();
213: } else {
214: clipString = "";
215: hasErrors = true;
216: }
217: if (!bottomValue.toString().equals("")) {
218: clipString += ", " + bottomValue.toString();
219: } else {
220: clipString = "";
221: hasErrors = true;
222: }
223:
224: if (!leftValue.toString().equals("")) {
225: clipString += ", " + leftValue.toString();
226: } else {
227: clipString = "";
228: hasErrors = true;
229: }
230: if (clipString.equals("") || hasErrors) {
231: clipString = "";
232: } else {
233: clipString = "rect(" + clipString.trim() + ")";
234: }
235: return clipString.trim();
236: }
237:
238: }
|