Skip to content

Commit 15be6fb

Browse files
committed
bug 60605: convert Workbook.SHEET_STATE_* to SheetVisibility enum
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1779558 13f79535-47bb-0310-9956-ffa450edef68
1 parent 85575f3 commit 15be6fb

File tree

8 files changed

+312
-138
lines changed

8 files changed

+312
-138
lines changed

src/java/org/apache/poi/hssf/model/InternalWorkbook.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
105105
import org.apache.poi.ss.formula.ptg.Ref3DPtg;
106106
import org.apache.poi.ss.formula.udf.UDFFinder;
107107
import org.apache.poi.ss.usermodel.BuiltinFormats;
108+
import org.apache.poi.ss.usermodel.SheetVisibility;
108109
import org.apache.poi.ss.usermodel.Workbook;
109110
import org.apache.poi.util.Internal;
110111
import org.apache.poi.util.LocaleUtil;
@@ -715,6 +716,27 @@ public boolean isSheetHidden(int sheetnum) {
715716
public boolean isSheetVeryHidden(int sheetnum) {
716717
return getBoundSheetRec(sheetnum).isVeryHidden();
717718
}
719+
720+
/**
721+
* Gets the hidden flag for a given sheet.
722+
* Note that a sheet could instead be
723+
* set to be very hidden, which is different
724+
* ({@link #isSheetVeryHidden(int)})
725+
*
726+
* @param sheetnum the sheet number (0 based)
727+
* @return True if sheet is hidden
728+
* @since 3.16 beta 2
729+
*/
730+
public SheetVisibility getSheetVisibility(int sheetnum) {
731+
final BoundSheetRecord bsr = getBoundSheetRec(sheetnum);
732+
if (bsr.isVeryHidden()) {
733+
return SheetVisibility.VERY_HIDDEN;
734+
}
735+
if (bsr.isHidden()) {
736+
return SheetVisibility.HIDDEN;
737+
}
738+
return SheetVisibility.VISIBLE;
739+
}
718740

719741
/**
720742
* Hide or unhide a sheet
@@ -723,32 +745,20 @@ public boolean isSheetVeryHidden(int sheetnum) {
723745
* @param hidden True to mark the sheet as hidden, false otherwise
724746
*/
725747
public void setSheetHidden(int sheetnum, boolean hidden) {
726-
getBoundSheetRec(sheetnum).setHidden(hidden);
748+
setSheetHidden(sheetnum, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE);
727749
}
728750

729751
/**
730752
* Hide or unhide a sheet.
731-
* 0 = not hidden
732-
* 1 = hidden
733-
* 2 = very hidden.
734753
*
735-
* @param sheetnum The sheet number
736-
* @param hidden 0 for not hidden, 1 for hidden, 2 for very hidden
754+
* @param sheetnum The sheet number
755+
* @param visibility the sheet visibility to set (visible, hidden, very hidden)
756+
* @since 3.16 beta 2
737757
*/
738-
public void setSheetHidden(int sheetnum, int hidden) {
758+
public void setSheetHidden(int sheetnum, SheetVisibility visibility) {
739759
BoundSheetRecord bsr = getBoundSheetRec(sheetnum);
740-
boolean h = false;
741-
boolean vh = false;
742-
if(hidden == 0) {
743-
} else if(hidden == 1) {
744-
h = true;
745-
} else if(hidden == 2) {
746-
vh = true;
747-
} else {
748-
throw new IllegalArgumentException("Invalid hidden flag " + hidden + " given, must be 0, 1 or 2");
749-
}
750-
bsr.setHidden(h);
751-
bsr.setVeryHidden(vh);
760+
bsr.setHidden(visibility == SheetVisibility.HIDDEN);
761+
bsr.setVeryHidden(visibility == SheetVisibility.VERY_HIDDEN);
752762
}
753763

754764

src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
100100
import org.apache.poi.ss.usermodel.Name;
101101
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
102102
import org.apache.poi.ss.usermodel.Sheet;
103+
import org.apache.poi.ss.usermodel.SheetVisibility;
103104
import org.apache.poi.ss.usermodel.Workbook;
104105
import org.apache.poi.ss.util.WorkbookUtil;
105106
import org.apache.poi.util.Configurator;
@@ -188,7 +189,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
188189
*/
189190
private MissingCellPolicy missingCellPolicy = MissingCellPolicy.RETURN_NULL_AND_BLANK;
190191

191-
private static POILogger log = POILogFactory.getLogger(HSSFWorkbook.class);
192+
private static final POILogger log = POILogFactory.getLogger(HSSFWorkbook.class);
192193

193194
/**
194195
* The locator of user-defined functions.
@@ -748,19 +749,46 @@ public boolean isSheetVeryHidden(int sheetIx) {
748749
validateSheetIndex(sheetIx);
749750
return workbook.isSheetVeryHidden(sheetIx);
750751
}
751-
752+
753+
@Override
754+
public SheetVisibility getSheetVisibility(int sheetIx) {
755+
return workbook.getSheetVisibility(sheetIx);
756+
}
752757

753758
@Override
754759
public void setSheetHidden(int sheetIx, boolean hidden) {
755-
validateSheetIndex(sheetIx);
756-
workbook.setSheetHidden(sheetIx, hidden);
760+
setSheetVisibility(sheetIx, hidden ? SheetVisibility.HIDDEN : SheetVisibility.VISIBLE);
757761
}
758762

763+
@Removal(version="3.18")
764+
@Deprecated
759765
@Override
760766
public void setSheetHidden(int sheetIx, int hidden) {
767+
switch (hidden) {
768+
case Workbook.SHEET_STATE_VISIBLE:
769+
setSheetVisibility(sheetIx, SheetVisibility.VISIBLE);
770+
break;
771+
case Workbook.SHEET_STATE_HIDDEN:
772+
setSheetVisibility(sheetIx, SheetVisibility.HIDDEN);
773+
break;
774+
case Workbook.SHEET_STATE_VERY_HIDDEN:
775+
setSheetVisibility(sheetIx, SheetVisibility.VERY_HIDDEN);
776+
break;
777+
default:
778+
throw new IllegalArgumentException("Invalid sheet state : " + hidden + "\n" +
779+
"Sheet state must beone of the Workbook.SHEET_STATE_* constants");
780+
}
781+
}
782+
783+
@Override
784+
public void setSheetVisibility(int sheetIx, SheetVisibility visibility) {
761785
validateSheetIndex(sheetIx);
762-
WorkbookUtil.validateSheetState(hidden);
763-
workbook.setSheetHidden(sheetIx, hidden);
786+
787+
/*if (visibility != SheetVisibility.VISIBLE && sheetIx == getActiveSheetIndex()) {
788+
throw new IllegalStateException("Cannot hide the active sheet. Change active sheet before hiding.");
789+
}*/
790+
791+
workbook.setSheetHidden(sheetIx, visibility);
764792
}
765793

766794
/** Returns the index of the sheet by his name
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* ====================================================================
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
==================================================================== */
17+
18+
package org.apache.poi.ss.usermodel;
19+
20+
/**
21+
* Specifies sheet visibility
22+
*
23+
* @see Workbook#getSheetVisibility(int)
24+
* @see Workbook#setSheetVisibility(int, SheetVisibility)
25+
*/
26+
public enum SheetVisibility {
27+
28+
/**
29+
* Indicates the sheet is visible.
30+
*/
31+
VISIBLE,
32+
/**
33+
* Indicates the book window is hidden, but can be shown by the user via the user interface.
34+
*/
35+
HIDDEN,
36+
37+
/**
38+
* Indicates the sheet is hidden and cannot be shown in the user interface (UI).
39+
*
40+
* <p>
41+
* In Excel this state is only available programmatically in VBA:
42+
* <code>ThisWorkbook.Sheets("MySheetName").Visible = xlSheetVeryHidden </code>
43+
* </p>
44+
*/
45+
VERY_HIDDEN;
46+
}

src/java/org/apache/poi/ss/usermodel/Workbook.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2626
import org.apache.poi.ss.SpreadsheetVersion;
2727
import org.apache.poi.ss.formula.udf.UDFFinder;
2828
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
29+
import org.apache.poi.util.Removal;
2930

3031
/**
3132
* High level representation of a Excel workbook. This is the first object most users
@@ -57,14 +58,20 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
5758
* Indicates the sheet is visible.
5859
*
5960
* @see #setSheetHidden(int, int)
61+
* @deprecated POI 3.16 beta 2. Use {@link SheetVisibility#VISIBLE} instead.
6062
*/
63+
@Deprecated
64+
@Removal(version="3.18")
6165
int SHEET_STATE_VISIBLE = 0;
6266

6367
/**
6468
* Indicates the book window is hidden, but can be shown by the user via the user interface.
6569
*
6670
* @see #setSheetHidden(int, int)
71+
* @deprecated POI 3.16 beta 2. Use {@link SheetVisibility#HIDDEN} instead.
6772
*/
73+
@Deprecated
74+
@Removal(version="3.18")
6875
int SHEET_STATE_HIDDEN = 1;
6976

7077
/**
@@ -76,7 +83,10 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
7683
* </p>
7784
*
7885
* @see #setSheetHidden(int, int)
86+
* @deprecated POI 3.16 beta 2. Use {@link SheetVisibility#VERY_HIDDEN} instead.
7987
*/
88+
@Deprecated
89+
@Removal(version="3.18")
8090
int SHEET_STATE_VERY_HIDDEN = 2;
8191

8292
/**
@@ -550,6 +560,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
550560
* </p>
551561
* @param sheetIx Number
552562
* @return <code>true</code> if sheet is hidden
563+
* @see #getSheetVisibility(int)
553564
*/
554565
boolean isSheetHidden(int sheetIx);
555566

@@ -561,6 +572,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
561572
* </p>
562573
* @param sheetIx sheet index to check
563574
* @return <code>true</code> if sheet is very hidden
575+
* @see #getSheetVisibility(int)
564576
*/
565577
boolean isSheetVeryHidden(int sheetIx);
566578

@@ -572,6 +584,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
572584
*
573585
* @param sheetIx the sheet index (0-based)
574586
* @param hidden True to mark the sheet as hidden, false otherwise
587+
* @see #setSheetVisibility(int, SheetVisibility)
575588
*/
576589
void setSheetHidden(int sheetIx, boolean hidden);
577590

@@ -593,8 +606,31 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
593606
* <code>Workbook.SHEET_STATE_HIDDEN</code>, or
594607
* <code>Workbook.SHEET_STATE_VERY_HIDDEN</code>.
595608
* @throws IllegalArgumentException if the supplied sheet index or state is invalid
609+
* @deprecated POI 3.16 beta 2. Use {@link #setSheetVisibility(int, SheetVisibility)} instead.
596610
*/
611+
@Removal(version="3.18")
597612
void setSheetHidden(int sheetIx, int hidden);
613+
614+
/**
615+
* Get the visibility (visible, hidden, very hidden) of a sheet in this workbook
616+
*
617+
* @param sheetIx the index of the sheet
618+
* @return the sheet visibility
619+
* @since POI 3.16 beta 2
620+
*/
621+
SheetVisibility getSheetVisibility(int sheetIx);
622+
623+
/**
624+
* Hide or unhide a sheet.
625+
*
626+
* Please note that the sheet currently set as active sheet (sheet 0 in a newly
627+
* created workbook or the one set via setActiveSheet()) cannot be hidden.
628+
*
629+
* @param sheetIx the sheet index (0-based)
630+
* @param visibility the sheet visibility to set
631+
* @since POI 3.16 beta 2
632+
*/
633+
void setSheetVisibility(int sheetIx, SheetVisibility visibility);
598634

599635
/**
600636
* Register a new toolpack in this workbook.

src/java/org/apache/poi/ss/util/WorkbookUtil.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717

1818
package org.apache.poi.ss.util;
1919

20+
import org.apache.poi.ss.usermodel.SheetVisibility;
2021
import org.apache.poi.ss.usermodel.Workbook;
22+
import org.apache.poi.util.Internal;
23+
import org.apache.poi.util.Removal;
2124

2225

2326
/**
@@ -169,14 +172,46 @@ public static void validateSheetName(String sheetName) {
169172
* {@link Workbook#SHEET_STATE_VISIBLE},
170173
* {@link Workbook#SHEET_STATE_HIDDEN} or
171174
* {@link Workbook#SHEET_STATE_VERY_HIDDEN}
175+
* @deprecated POI 3.16 beta 2. Use {@link org.apache.poi.ss.usermodel.SheetVisibility} instead.
172176
*/
177+
@Removal(version="3.18")
178+
@Deprecated
173179
public static void validateSheetState(int state) {
174180
switch(state){
175181
case Workbook.SHEET_STATE_VISIBLE: break;
176182
case Workbook.SHEET_STATE_HIDDEN: break;
177183
case Workbook.SHEET_STATE_VERY_HIDDEN: break;
178-
default: throw new IllegalArgumentException("Ivalid sheet state : " + state + "\n" +
184+
default: throw new IllegalArgumentException("Invalid sheet state : " + state + "\n" +
179185
"Sheet state must beone of the Workbook.SHEET_STATE_* constants");
180186
}
181-
}
187+
}
188+
189+
@Internal(since="3.16 beta 2")
190+
public static int getNextActiveSheetDueToSheetHiding(Workbook wb, int sheetIx) {
191+
if (sheetIx == wb.getActiveSheetIndex()) {
192+
// activate next sheet
193+
// if last sheet in workbook, the previous visible sheet should be activated
194+
final int count = wb.getNumberOfSheets();
195+
for (int i=sheetIx+1; i < count; i++) {
196+
// get the next visible sheet in this workbook
197+
if (SheetVisibility.VISIBLE == wb.getSheetVisibility(i)) {
198+
return i;
199+
}
200+
}
201+
202+
// if there are no sheets to the right or all sheets to the right are hidden, activate a sheet to the left
203+
for (int i=sheetIx-1; i < count; i--) {
204+
if (SheetVisibility.VISIBLE == wb.getSheetVisibility(i)) {
205+
return i;
206+
}
207+
}
208+
209+
// there are no other visible sheets in this workbook
210+
return -1;
211+
//throw new IllegalStateException("Cannot hide sheet " + sheetIx + ". Workbook must contain at least 1 other visible sheet.");
212+
}
213+
else {
214+
return sheetIx;
215+
}
216+
}
182217
}

0 commit comments

Comments
 (0)