xref: /AOO41X/main/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/sxc/SheetSettings.java (revision 0c0e82a55dc5b7baa849907647dcb88a54f5f573)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.sxc;
25 
26 import org.w3c.dom.NamedNodeMap;
27 import org.w3c.dom.NodeList;
28 import org.w3c.dom.Node;
29 import org.w3c.dom.Element;
30 import java.awt.Point;
31 
32 import org.openoffice.xmerge.converter.xml.OfficeConstants;
33 
34 /**
35  * This is a class representing the different attributes for a worksheet
36  * contained in settings.xml.
37  *
38  * @author Martin Maher
39  */
40 public class SheetSettings implements OfficeConstants {
41 
42     /**  A w3c <code>Document</code>. */
43     private org.w3c.dom.Document settings = null;
44 
45     private String  sheetName;
46     private int     cursorX     = 0;
47     private int     cursorY     = 0;
48     private int     splitTypeX;
49     private int     splitTypeY;
50     private int     splitPointX = 0;
51     private int     splitPointY = 0;
52     private int     posLeft     = 0;
53     private int     posRight    = 0;
54     private int     posBottom   = 0;
55     private int     posTop      = 0;
56     private int     paneNumber  = 2;
57 
58     final public static int NONE    = 0x00;
59     final public static int SPLIT   = 0x01;
60     final public static int FREEZE  = 0x02;
61 
62 
63     /**
64      * Default Constructor for a <code>ColumnRowInfo</code>
65      *
66      */
SheetSettings()67     public SheetSettings() {
68     }
69 
70     /**
71      * Constructor that takes a <code>Node</code> to build a <code>SheetSettings</code>
72      *
73      * @param root XML Node to read from
74      */
SheetSettings(Node root)75     public SheetSettings(Node root) {
76         readNode(root);
77     }
78 
79     /**
80      * Constructor for a <code>ColumnRowInfo</code>
81      *
82      * @param name
83      */
SheetSettings(String name)84     public SheetSettings(String name) {
85         sheetName = name;
86     }
87 
88     /**
89      * sets the position of the acitve cell
90      *
91      * @param activeCell the current curor position
92      */
setCursor(Point activeCell)93     public void setCursor(Point activeCell) {
94 
95         cursorX = (int) activeCell.getX();
96         cursorY = (int) activeCell.getY();
97     }
98 
99     /**
100      * Gets the position of the acitve cell
101      *
102      * @return The position as a <code>Point</code>
103      */
getCursor()104     public Point getCursor() {
105 
106         return (new Point(cursorX, cursorY));
107     }
108 
109     /**
110      * Sets the position  of the freeze
111      *
112      * @param splitPoint the point at where the split occurs
113      */
setFreeze(Point splitPoint)114     public void setFreeze(Point splitPoint) {
115 
116         splitTypeX      = FREEZE;
117         splitTypeY      = FREEZE;
118         splitPointX     = (int) splitPoint.getX();
119         splitPointY     = (int) splitPoint.getY();
120     }
121 
122     /**
123      * Sets the position of the split
124      *
125      * @param splitPoint the point at where the split occurs
126      */
setSplit(Point splitPoint)127     public void setSplit(Point splitPoint) {
128 
129         splitTypeX      = SPLIT;
130         splitTypeY      = SPLIT;
131         splitPointX     = (int) splitPoint.getX();
132         splitPointY     = (int) splitPoint.getY();
133     }
134 
135     /**
136      * sets the position and type of the split
137      *
138      * @return The position as a <code>Point</code> where the split occurs
139      */
getSplit()140     public Point getSplit() {
141 
142         return (new Point(splitPointX, splitPointY));
143     }
144 
145     /**
146      * sets the position and type of the split
147      *
148      * @return The position as a <code>Point</code> where the split occurs
149      */
getSplitType()150     public Point getSplitType() {
151 
152         return (new Point(splitTypeX, splitTypeY));
153     }
154 
155     /**
156      * Sets the top row visible in the lower pane and the leftmost column
157      * visibile in the right pane.
158      *
159      * @param top The top row visible in the lower pane
160      * @param left The leftmost column visibile in the right pane
161      */
setTopLeft(int top, int left)162     public void setTopLeft(int top, int left) {
163 
164         posLeft = left;
165         posTop = top;
166     }
167 
168     /**
169      * Gets the the leftmost column visibile in the right pane.
170      *
171      * @return the 0-based index to the column
172      */
getLeft()173     public int getLeft() {
174 
175         return posLeft;
176     }
177 
178     /**
179      * Gets the top row visible in the lower pane.
180      *
181      */
getTop()182     public int getTop() {
183 
184         return posTop;
185     }
186 
187     /**
188      * Gets the active Panel
189      * 0 - Bottom Right, 1 - Top Right
190      * 2 - Bottom Left, 3 - Top Left
191      *
192      * @return int representing the active panel
193      */
getPaneNumber()194     public int getPaneNumber() {
195 
196         return paneNumber;
197     }
198 
199     /**
200      * Sets the sheetname this settings object applies to
201      *
202      * @param sheetName the name of the worksheet
203      */
setSheetName(String sheetName)204     public void setSheetName(String sheetName) {
205 
206         this.sheetName = sheetName;
207 
208     }
209 
210     /**
211      * Sets the active pane number
212      * 0 - Bottom Right, 1 - Top Right
213      * 2 - Bottom Left, 3 - Top Left
214      *
215      * @param paneNumber the pane number
216      */
setPaneNumber(int paneNumber)217     public void setPaneNumber(int paneNumber) {
218 
219         this.paneNumber = paneNumber;
220     }
221 
222     /**
223      * Gets the name of the worksheet these <code>Settings</code> apply to
224      *
225      * @return the name of the worksheet
226      */
getSheetName()227     public String getSheetName() {
228 
229         return sheetName;
230     }
231 
232     /**
233      * Adds an XML entry for a particular setting
234      *
235      * @param root the root node at which to add the xml entry
236      * @param attribute the name of the attribute to add
237      * @param type the attribute type (int, short etc)
238      * @param value the value of the attribute
239      */
addConfigItem(Node root, String attribute, String type, String value)240     private void addConfigItem(Node root, String attribute, String type, String value) {
241 
242         Element configItem = settings.createElement(TAG_CONFIG_ITEM);
243         configItem.setAttribute(ATTRIBUTE_CONFIG_NAME, attribute);
244         configItem.setAttribute(ATTRIBUTE_CONFIG_TYPE, type);
245 
246         configItem.appendChild(settings.createTextNode(value));
247 
248         root.appendChild(configItem);
249     }
250 
251     /**
252      * Writes out a settings.xml entry for this SheetSettings object
253      *
254      * @param settings a <code>Document</code> object representing the settings.xml
255      * @param root the root xml node to add to
256      */
writeNode(org.w3c.dom.Document settings, Node root)257     public void writeNode(org.w3c.dom.Document settings, Node root) {
258 
259         this.settings = settings;
260         Element configItemMapEntry      = (Element) settings.createElement(TAG_CONFIG_ITEM_MAP_ENTRY);
261         configItemMapEntry.setAttribute(ATTRIBUTE_CONFIG_NAME, getSheetName());
262         addConfigItem(configItemMapEntry, "CursorPositionX", "int", Integer.toString(cursorX));
263         addConfigItem(configItemMapEntry, "CursorPositionY", "int", Integer.toString(cursorY));
264 
265         String splitMode = Integer.toString(splitTypeX);
266         if(splitPointX==0) {
267             splitMode = "0";
268         }
269         addConfigItem(configItemMapEntry, "HorizontalSplitMode", "short", splitMode);
270 
271         splitMode = Integer.toString(splitTypeY);
272         if(splitPointY==0) {
273             splitMode = "0";
274         }
275         addConfigItem(configItemMapEntry, "VerticalSplitMode", "short", splitMode);
276 
277         addConfigItem(configItemMapEntry, "HorizontalSplitPosition", "int", Integer.toString(splitPointX));
278         addConfigItem(configItemMapEntry, "VerticalSplitPosition", "int", Integer.toString(splitPointY));
279         addConfigItem(configItemMapEntry, "ActiveSplitRange", "short", Integer.toString(paneNumber));
280 
281         addConfigItem(configItemMapEntry, "PositionLeft", "int", "0");
282         addConfigItem(configItemMapEntry, "PositionRight", "int", Integer.toString(posLeft));
283         addConfigItem(configItemMapEntry, "PositionTop", "int", "0");
284         addConfigItem(configItemMapEntry, "PositionBottom", "int", Integer.toString(posTop));
285         root.appendChild(configItemMapEntry);
286     }
287 
288     /**
289      * Sets a variable based on a String value read from XML
290      *
291      * @param name xml name of the attribute to set
292      * @param value String value fo the attribute
293      */
addAttribute(String name, String value)294     public void addAttribute(String name, String value) {
295 
296         if(name.equals("CursorPositionX")) {
297             cursorX = Integer.parseInt(value);
298         } else if(name.equals("CursorPositionY")) {
299             cursorY = Integer.parseInt(value);
300 
301         } else if(name.equals("HorizontalSplitPosition")) {
302             splitPointX = Integer.parseInt(value);
303         } else if(name.equals("VerticalSplitPosition")) {
304             splitPointY = Integer.parseInt(value);
305         } else if(name.equals("ActiveSplitRange")) {
306             paneNumber = Integer.parseInt(value);
307 
308         } else if(name.equals("PositionRight")) {
309             posLeft = Integer.parseInt(value);
310         } else if(name.equals("PositionBottom")) {
311             posTop = Integer.parseInt(value);
312 
313         } else if(name.equals("HorizontalSplitMode")) {
314             splitTypeX = Integer.parseInt(value);
315         } else if(name.equals("VerticalSplitMode")) {
316             splitTypeY = Integer.parseInt(value);
317         }
318     }
319 
320     /**
321      * Reads document settings from xml and inits SheetSettings variables
322      *
323      * @param root XML Node to read from
324      */
readNode(Node root)325     public void readNode(Node root) {
326 
327         NamedNodeMap sheetAtt = root.getAttributes();
328 
329         Node sheetNameNode = sheetAtt.getNamedItem(ATTRIBUTE_CONFIG_NAME);
330 
331         sheetName = sheetNameNode.getNodeValue();
332 
333         if (root.hasChildNodes()) {
334 
335             NodeList nodeList = root.getChildNodes();
336             int len = nodeList.getLength();
337             for (int i = 0; i < len; i++) {
338                 Node child = nodeList.item(i);
339 
340                 if (child.getNodeType() == Node.ELEMENT_NODE) {
341                     String nodeName = child.getNodeName();
342 
343                     if (nodeName.equals(TAG_CONFIG_ITEM)) {
344 
345                         NamedNodeMap cellAtt = child.getAttributes();
346 
347                         Node configNameNode =
348                             cellAtt.getNamedItem(ATTRIBUTE_CONFIG_NAME);
349 
350                         String name = configNameNode.getNodeValue();
351                         NodeList nodeList2 = child.getChildNodes();
352                         int len2 = nodeList2.getLength();
353                         String s = "";
354                         for (int j = 0; j < len2; j++) {
355                             Node child2 = nodeList2.item(j);
356                             if (child2.getNodeType() == Node.TEXT_NODE) {
357                                 s = child2.getNodeValue();
358                             }
359                         }
360                         addAttribute(name, s);
361                     }
362                 }
363             }
364         }
365     }
366 }
367