001package com.hfg.xml.msofficexml.xlsx.spreadsheetml;
002
003import com.hfg.exception.InvalidValueException;
004import com.hfg.xml.XMLTag;
005import com.hfg.xml.msofficexml.xlsx.CellRef;
006import com.hfg.xml.msofficexml.xlsx.ExcelIndexedColor;
007import com.hfg.xml.msofficexml.xlsx.Xlsx;
008
009import java.awt.Color;
010
011//------------------------------------------------------------------------------
012/**
013 Represents an Office Open XML sheet view (<ssml:sheetView>) tag.
014
015 @author J. Alex Taylor, hairyfatguy.com
016 */
017//------------------------------------------------------------------------------
018// com.hfg XML/HTML Coding Library
019//
020// This library is free software; you can redistribute it and/or
021// modify it under the terms of the GNU Lesser General Public
022// License as published by the Free Software Foundation; either
023// version 2.1 of the License, or (at your option) any later version.
024//
025// This library is distributed in the hope that it will be useful,
026// but WITHOUT ANY WARRANTY; without even the implied warranty of
027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
028// Lesser General Public License for more details.
029//
030// You should have received a copy of the GNU Lesser General Public
031// License along with this library; if not, write to the Free Software
032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
033//
034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
035// jataylor@hairyfatguy.com
036//------------------------------------------------------------------------------
037// Ex: <sheetView tabSelected="1" view="pageLayout" workbookViewId="0"><selection activeCell="B3" sqref="B3"/></sheetView>
038public class SsmlSheetView extends SsmlXMLTag
039{
040   //###########################################################################
041   // PRIVATE FIELDS
042   //###########################################################################
043
044   private XMLTag   mSelectionTag;
045   private SsmlPane mPane;
046
047   //###########################################################################
048   // CONSTRUCTORS
049   //###########################################################################
050
051   //---------------------------------------------------------------------------
052   public SsmlSheetView(Xlsx inXlsx)
053   {
054      super(SsmlXML.SHEET_VIEW, inXlsx);
055
056      // Set some default attributes
057//      setTabSelected(true);  // This was causing Excel to say: "This action won't work for multiple selections." when trying to copy and paste content into another Workbook
058      setViewType(SsmlSheetViewType.normal);
059      setWorkbookViewId(0);
060      setSelection(new CellRef("A1"));
061   }
062
063   //###########################################################################
064   // PUBLIC METHODS
065   //###########################################################################
066
067   //---------------------------------------------------------------------------
068   /**
069    Specifies the view type.
070    Default value is "normal".
071    */
072   public SsmlSheetView setViewType(SsmlSheetViewType inValue)
073   {
074      if (inValue != null)
075      {
076         setAttribute(SsmlXML.VIEW_ATT, inValue.name());
077      }
078      else
079      {
080         removeAttribute(SsmlXML.VIEW_ATT);
081      }
082
083      return this;
084   }
085
086   //---------------------------------------------------------------------------
087   /**
088    Specifies the location of the top left visible cell in the bottom right pane (when in Left-to-Right mode).
089    */
090   public SsmlSheetView setTopLeftCell(CellRef inValue)
091   {
092      if (inValue != null)
093      {
094         setAttribute(SsmlXML.TOP_LEFT_CELL_ATT, inValue.toString());
095      }
096      else
097      {
098         removeAttribute(SsmlXML.TOP_LEFT_CELL_ATT);
099      }
100
101      return this;
102   }
103
104   //---------------------------------------------------------------------------
105   /**
106    Specifies whether the sheet is in 'right to left' display mode.
107    When in this mode, Column A is on the far right, Column B ;is one column left of Column A, and so on.
108    Also, information in cells is displayed in the Right to Left format.
109    Default value is "false".
110    */
111   public SsmlSheetView setRightToLeft(boolean inValue)
112   {
113      setAttribute(SsmlXML.RIGHT_TO_LEFT_ATT, inValue);
114      return this;
115   }
116
117   //---------------------------------------------------------------------------
118   /**
119    Specifies whether or not the sheet tab is selected.
120    Default value is "false".
121    */
122   public SsmlSheetView setTabSelected(boolean inValue)
123   {
124      setAttribute(SsmlXML.TAB_SELECTED_ATT, inValue);
125      return this;
126   }
127
128   //---------------------------------------------------------------------------
129   /**
130    Specifies whether or not the ruler is displayed in Page Layout View.
131    Default value is "true".
132    */
133   public SsmlSheetView showRuler(boolean inValue)
134   {
135      setAttribute(SsmlXML.SHOW_RULER_ATT, inValue);
136      return this;
137   }
138
139   //---------------------------------------------------------------------------
140   /**
141    Specifies whether or not to display zero values.
142    Default value is "true".
143    */
144   public SsmlSheetView setShowZeros(boolean inValue)
145   {
146      setAttribute(SsmlXML.SHOW_ZEROS_ATT, inValue);
147      return this;
148   }
149
150   //---------------------------------------------------------------------------
151   /**
152    Specifies whether or not to display formulas.
153    Default value is "false".
154    */
155   public SsmlSheetView setShowFormulas(boolean inValue)
156   {
157      setAttribute(SsmlXML.SHOW_FORMULAS_ATT, inValue);
158      return this;
159   }
160
161   //---------------------------------------------------------------------------
162   /**
163    Specifies whether or not to display grid lines.
164    Default value is "true".
165    */
166   public SsmlSheetView setShowGridLines(boolean inValue)
167   {
168      setAttribute(SsmlXML.SHOW_GRID_LINES_ATT, inValue);
169      return this;
170   }
171
172   //---------------------------------------------------------------------------
173   /**
174    Specifies whether or not page layout view shall display margins.
175    False means do not display left, right, top (header), and bottom (footer) margins
176    (even when there is data in the header or footer).
177    Default value is "true".
178    */
179   public SsmlSheetView setShowWhiteSpace(boolean inValue)
180   {
181      setAttribute(SsmlXML.SHOW_WHITE_SPACE_ATT, inValue);
182      return this;
183   }
184
185   //---------------------------------------------------------------------------
186   /**
187    Specifies whether or not the sheet has outline symbols visible.
188    This flag shall always override SheetPr element's outlinePr child element
189    whose attribute is named showOutlineSymbols when there is a conflict.
190    Default value is "true".
191    */
192   public SsmlSheetView setShowOutlineSymbols(boolean inValue)
193   {
194      setAttribute(SsmlXML.SHOW_OUTLINE_SYMBOLS_ATT, inValue);
195      return this;
196   }
197
198   //---------------------------------------------------------------------------
199   /**
200    Specifies whether or not the consuming application should use the default grid
201    lines color (system dependent). Overrides any color specified in colorId.
202    Default value is "true".
203    */
204   public SsmlSheetView setUseDefaultGridColor(boolean inValue)
205   {
206      setAttribute(SsmlXML.DEFAULT_GRID_COLOR, inValue);
207      return this;
208   }
209
210   //---------------------------------------------------------------------------
211   /**
212    Specifies whether or not to display row and column headers.
213    Default value is "true".
214    */
215   public SsmlSheetView setShowRowColHeaders(boolean inValue)
216   {
217      setAttribute(SsmlXML.SHOW_ROW_COL_HEADERS_ATT, inValue);
218      return this;
219   }
220
221   //---------------------------------------------------------------------------
222   /**
223    Specifies whether or not to enable window protection.
224    Default value is "false".
225    */
226   public SsmlSheetView setWindowProtection(boolean inValue)
227   {
228      setAttribute(SsmlXML.WINDOW_PROTECTION_ATT, inValue);
229      return this;
230   }
231
232   //---------------------------------------------------------------------------
233   /**
234    Specifies the color value for row/column text headings and gridlines.
235    The color specified will be mapped to the closest Excel indexed color value.
236    */
237   public SsmlSheetView setColor(Color inValue)
238   {
239      if (inValue != null)
240      {
241         setAttribute(SsmlXML.COLOR_ID_ATT, (inValue instanceof ExcelIndexedColor ? (ExcelIndexedColor) inValue : ExcelIndexedColor.findNearestMatch(inValue)).getIndex());
242         // The default coloring needs to be turned off for this to work.
243         setUseDefaultGridColor(false);
244      }
245      else
246      {
247         removeAttribute(SsmlXML.COLOR_ID_ATT);
248         setUseDefaultGridColor(true);
249      }
250
251      return this;
252   }
253
254   //---------------------------------------------------------------------------
255   /**
256    Specifies the window zoom magnification for current view representing percent values.
257    This attribute is restricted to values ranging from 10 to 400. Horizontal &amp; Vertical scale together.
258    Default value is 100.
259    */
260   public SsmlSheetView setZoomScale(Integer inValue)
261   {
262      if (inValue != null)
263      {
264         if (inValue < 10
265               || inValue > 400)
266         {
267            throw new InvalidValueException("The zoom scale value must be between 10 and 400 (%).");
268         }
269
270         setAttribute(SsmlXML.ZOOM_SCALE_ATT, inValue);
271      }
272      else
273      {
274         removeAttribute(SsmlXML.ZOOM_SCALE_ATT);
275      }
276
277      return this;
278   }
279
280   //---------------------------------------------------------------------------
281   /**
282    Specifies the zoom magnification when in normal view, representing percent values.
283    This attribute is restricted to values ranging from 10 to 400. Horizontal &amp; Vertical scale together.
284    Default value is 0.
285    */
286   public SsmlSheetView setNormalViewZoomScale(Integer inValue)
287   {
288      if (inValue != null)
289      {
290         if (inValue < 10
291             || inValue > 400)
292         {
293            throw new InvalidValueException("The zoom scale value must be between 10 and 400 (%).");
294         }
295
296         setAttribute(SsmlXML.ZOOM_SCALE_NORMAL_ATT, inValue);
297      }
298      else
299      {
300         removeAttribute(SsmlXML.ZOOM_SCALE_NORMAL_ATT);
301      }
302
303      return this;
304   }
305
306   //---------------------------------------------------------------------------
307   /**
308    Specifies the zoom magnification when in page layout view, representing percent values.
309    This attribute is restricted to values ranging from 10 to 400. Horizontal &amp; Vertical scale together.
310    Default value is 0.
311    */
312   public SsmlSheetView setPageLayoutViewZoomScale(Integer inValue)
313   {
314      if (inValue != null)
315      {
316         if (inValue < 10
317             || inValue > 400)
318         {
319            throw new InvalidValueException("The zoom scale value must be between 10 and 400 (%).");
320         }
321
322         setAttribute(SsmlXML.ZOOM_SCALE_PAGE_LAYOUT_VIEW_ATT, inValue);
323      }
324      else
325      {
326         removeAttribute(SsmlXML.ZOOM_SCALE_PAGE_LAYOUT_VIEW_ATT);
327      }
328
329      return this;
330   }
331
332   //---------------------------------------------------------------------------
333   /**
334    Specifies the zoom magnification when in page break preview, representing percent values.
335    This attribute is restricted to values ranging from 10 to 400. Horizontal &amp; Vertical scale together.
336    Default value is 0.
337    */
338   public SsmlSheetView setSheetLayoutViewZoomScale(Integer inValue)
339   {
340      if (inValue != null)
341      {
342         if (inValue < 10
343             || inValue > 400)
344         {
345            throw new InvalidValueException("The zoom scale value must be between 10 and 400 (%).");
346         }
347
348         setAttribute(SsmlXML.ZOOM_SCALE_SHEET_LAYOUT_VIEW_ATT, inValue);
349      }
350      else
351      {
352         removeAttribute(SsmlXML.ZOOM_SCALE_SHEET_LAYOUT_VIEW_ATT);
353      }
354
355      return this;
356   }
357
358   //---------------------------------------------------------------------------
359   public SsmlSheetView setWorkbookViewId(Integer inValue)
360   {
361      if (inValue != null)
362      {
363         setAttribute(SsmlXML.WORKBOOK_VIEW_ID_ATT, inValue);
364      }
365      else
366      {
367         removeAttribute(SsmlXML.WORKBOOK_VIEW_ID_ATT);
368      }
369
370      return this;
371   }
372
373   //---------------------------------------------------------------------------
374   public SsmlSheetView setSelection(CellRef inValue)
375   {
376      if (null == mSelectionTag)
377      {
378         // Check it it has been added via addSubtag()...
379         mSelectionTag = getOptionalSubtagByName(SsmlXML.SELECTION);
380         if (null == mSelectionTag)
381         {
382            mSelectionTag = new XMLTag(SsmlXML.SELECTION);
383            addSubtag(mSelectionTag);
384         }
385      }
386
387      mSelectionTag.setAttribute(SsmlXML.ACTIVE_CELL_ATT, inValue.toString());
388      mSelectionTag.setAttribute(SsmlXML.SQUARE_REF_ATT, inValue.toString());
389
390      return this;
391   }
392
393   //---------------------------------------------------------------------------
394   public SsmlPane getPane()
395   {
396      if (null == mPane)
397      {
398         // Check it it has been added via addSubtag()...
399         mPane = getOptionalSubtagByName(SsmlXML.PANE);
400         if (null == mPane)
401         {
402            mPane = new SsmlPane(getParentDoc());
403            addSubtag(mPane);
404         }
405      }
406
407      return mPane;
408   }
409}