001/** 002 * DialogItem -- An item for a dialog that can have many separate elements. 003 * 004 * Copyright (C) 2009-2025, by Joseph A. Huwaldt. All rights reserved. 005 * 006 * This library is free software; you can redistribute it and/or modify it under the terms 007 * of the GNU Lesser General Public License as published by the Free Software Foundation; 008 * either version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, but WITHOUT ANY 011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 012 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public License along with 015 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - 016 * Suite 330, Boston, MA 02111-1307, USA. Or visit: http://www.gnu.org/licenses/lgpl.html 017 */ 018package geomss.ui; 019 020import static java.util.Objects.requireNonNull; 021import javax.measure.unit.Unit; 022 023/** 024 * A container for for items to display in a dialog. 025 * 026 * <p> Modified by: Joseph A. Huwaldt </p> 027 * 028 * @author Joseph A. Huwaldt, Date: May 5, 2009 029 * @version February 22, 2025 030 */ 031public class DialogItem implements Cloneable { 032 033 private String _prefixStr; 034 private Object _element; 035 private String _suffixStr = null; 036 private Unit[] _unitList = null; 037 private boolean _fileLoad = true; 038 private String _fileExtension = null; 039 040 /** 041 * Construct a new and empty DialogItem. 042 */ 043 public DialogItem() { 044 } 045 046 /** 047 * Construct a new DialogItem with the specified elements. 048 * 049 * @param prefix The prefix for the dialog to display. 050 * @param element The the item to display in the dialog. 051 */ 052 public DialogItem(String prefix, Object element) { 053 _prefixStr = requireNonNull(prefix); 054 _element = requireNonNull(element); 055 } 056 057 /** 058 * Construct a new DialogItem with the specified elements. 059 * 060 * @param prefix The prefix for the item displayed. 061 * @param element The item to display in the dialog. 062 * @param suffix The suffix for the item displayed. 063 */ 064 public DialogItem(String prefix, Object element, String suffix) { 065 _prefixStr = requireNonNull(prefix); 066 _element = requireNonNull(element); 067 _suffixStr = requireNonNull(suffix); 068 } 069 070 /** 071 * Construct a new DialogItem with the specified elements. 072 * 073 * @param prefix The prefix for the item displayed. 074 * @param element The item to display in the dialog. 075 * @param unitList The list of units to display with the item. 076 */ 077 public DialogItem(String prefix, Object element, Unit[] unitList) { 078 _prefixStr = requireNonNull(prefix); 079 _element = requireNonNull(element); 080 _unitList = requireNonNull(unitList); 081 } 082 083 /** 084 * Return the prefix String. 085 * 086 * @return the prefix 087 */ 088 public String getPrefix() { 089 return _prefixStr; 090 } 091 092 /** 093 * Set the prefix String. 094 * 095 * @param prefix the prefix to display with the item. 096 */ 097 public void setPrefix(String prefix) { 098 _prefixStr = requireNonNull(prefix); 099 } 100 101 /** 102 * Return the element to be edited. 103 * 104 * @return the element or item 105 */ 106 public Object getElement() { 107 return _element; 108 } 109 110 /** 111 * Set the element to be edited. 112 * 113 * @param element the item to display in the dialog. 114 */ 115 public void setElement(Object element) { 116 _element = requireNonNull(element); 117 } 118 119 /** 120 * Return the suffix String. 121 * 122 * @return the suffix 123 */ 124 public String getSuffix() { 125 return _suffixStr; 126 } 127 128 /** 129 * Set the suffix string. 130 * 131 * @param suffix the suffix to place after the item in the dialog. 132 */ 133 public void setSuffix(String suffix) { 134 _suffixStr = requireNonNull(suffix); 135 } 136 137 /** 138 * Return the array of units. 139 * 140 * @return the array of units displayed with the item in the dialog. 141 */ 142 public Unit[] getUnitList() { 143 return _unitList; 144 } 145 146 /** 147 * Set the array of units. 148 * 149 * @param units the array of units to display with the item. 150 */ 151 public void setUnitList(Unit[] units) { 152 _unitList = requireNonNull(units); 153 } 154 155 /** 156 * Set the file load/save option (if the element is of type File). 157 * 158 * @param load Set to <code>true</code> to use a "load file" dialog and 159 * <code>false</code> to use a "save file" dialog. 160 */ 161 public void setLoadFile(boolean load) { 162 _fileLoad = load; 163 } 164 165 /** 166 * Returns the load/save option (true to use a load file dialog and false to use a 167 * save file dialog). 168 * 169 * @return The value true to use a load file dialog and false to use a 170 * save file dialog. 171 */ 172 public boolean isLoadFile() { 173 return _fileLoad; 174 } 175 176 /** 177 * Set the file extension to enforce when selecting a file for saving. 178 * 179 * @param extension The file extension to enforce when selecting a file for saving. 180 */ 181 public void setFileExtension(String extension) { 182 _fileExtension = extension; 183 } 184 185 /** 186 * Returns the file extension requested for saving a file (or null if none has been 187 * set). 188 * 189 * @return The file extension requested for saving a file or null if none has been set. 190 */ 191 public String getFileExtension() { 192 return _fileExtension; 193 } 194 195 /** 196 * Returns a shallow copy of this DialogItem instance. 197 * 198 * @return a clone of this DialogItem instance 199 */ 200 @Override 201 @SuppressWarnings("CloneDeclaresCloneNotSupported") 202 public Object clone() { 203 DialogItem result = null; 204 205 try { 206 // Make a shallow copy. 207 result = (DialogItem)super.clone(); 208 209 } catch (Exception e) { 210 // Shouldn't happen if this object implements Cloneable. 211 e.printStackTrace(); 212 } 213 214 return result; 215 } 216}