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 package com.sun.star.wizards.web; 24 25 import javax.swing.DefaultListModel; 26 27 import com.sun.star.awt.Size; 28 import com.sun.star.lang.XMultiServiceFactory; 29 import com.sun.star.wizards.common.ConfigSet; 30 import com.sun.star.wizards.common.Configuration; 31 import com.sun.star.wizards.common.FileAccess; 32 import com.sun.star.wizards.common.PropertyNames; 33 import com.sun.star.wizards.common.SystemDialog; 34 import com.sun.star.wizards.ui.ImageList; 35 import com.sun.star.wizards.web.data.CGImage; 36 import com.sun.star.wizards.web.data.CGSettings; 37 38 /** 39 * @author rpiterman 40 * 41 * To change the template for this generated type comment go to 42 * Window>Preferences>Java>Code Generation>Code and Comments 43 */ 44 public class BackgroundsDialog extends ImageListDialog 45 { 46 47 private FileAccess fileAccess; 48 private SystemDialog sd; 49 private CGSettings settings; 50 51 /** 52 * @param xmsf 53 */ BackgroundsDialog( XMultiServiceFactory xmsf, ConfigSet set_, WebWizardDialogResources resources)54 public BackgroundsDialog( 55 XMultiServiceFactory xmsf, 56 ConfigSet set_, WebWizardDialogResources resources) throws Exception 57 { 58 59 super(xmsf, WWHID.HID_BG, new String[] 60 { 61 resources.resBackgroundsDialog, 62 resources.resBackgroundsDialogCaption, 63 resources.resOK, 64 resources.resCancel, 65 resources.resHelp, 66 resources.resDeselect, 67 resources.resOther, 68 resources.resCounter 69 }); 70 71 sd = SystemDialog.createOpenDialog(xmsf); 72 sd.addFilter(resources.resImages, "*.jpg;*.jpeg;*.jpe;*.gif", true); 73 sd.addFilter(resources.resAllFiles, "*.*", false); 74 75 settings = (CGSettings) set_.root; 76 77 fileAccess = new FileAccess(xmsf); 78 il.setListModel(new Model(set_)); 79 il.setImageSize(new Size(40, 40)); 80 il.setRenderer(new BGRenderer(0)); 81 build(); 82 83 84 } 85 86 /** 87 * trigered when the user clicks the "other" button. 88 * opens a "file open" dialog, adds the selected 89 * image to the list and to the web wizard configuration, 90 * and then jumps to the new image, selecting it in the list. 91 * @see add(String) 92 */ other()93 public void other() 94 { 95 String filename[] = sd.callOpenDialog(false, settings.cp_DefaultSession.cp_InDirectory); 96 if (filename != null && filename.length > 0 && filename[0] != null) 97 { 98 settings.cp_DefaultSession.cp_InDirectory = FileAccess.getParentDir(filename[0]); 99 int i = add(filename[0]); 100 il.setSelected(i); 101 il.display(i); 102 } 103 } 104 105 /** 106 * adds the given image to the image list (to the model) 107 * and to the web wizard configuration. 108 * @param s 109 * @return 110 */ add(String s)111 private int add(String s) 112 { 113 114 //first i check the item does not already exists in the list... 115 for (int i = 0; i < il.getListModel().getSize(); i++) 116 { 117 if (il.getListModel().getElementAt(i).equals(s)) 118 { 119 return i; 120 } 121 } 122 ((DefaultListModel) il.getListModel()).addElement(s); 123 try 124 { 125 Object configView = Configuration.getConfigurationRoot(xMSF, FileAccess.connectURLs(WebWizardConst.CONFIG_PATH, "BackgroundImages"), true); 126 int i = Configuration.getChildrenNames(configView).length + 1; 127 Object o = Configuration.addConfigNode(configView, PropertyNames.EMPTY_STRING + i); 128 Configuration.set(s, "Href", o); 129 Configuration.commit(configView); 130 } 131 catch (Exception ex) 132 { 133 ex.printStackTrace(); 134 } 135 136 return il.getListModel().getSize() - 1; 137 138 } 139 140 /** 141 * an ImageList Imagerenderer implemtation. 142 * The image URL is the object given from the list model. 143 * the image name, got from the "render" method is 144 * the filename portion of the url. 145 * @author rpiterman 146 * 147 */ 148 private class BGRenderer implements ImageList.IImageRenderer 149 { 150 151 private int cut; 152 BGRenderer(int cut_)153 public BGRenderer(int cut_) 154 { 155 cut = cut_; 156 } 157 getImageUrls(Object listItem)158 public Object[] getImageUrls(Object listItem) 159 { 160 Object[] sRetUrls; 161 if (listItem != null) 162 { 163 sRetUrls = new Object[1]; 164 sRetUrls[0] = listItem; 165 return sRetUrls; 166 } 167 return null; 168 } 169 render(Object object)170 public String render(Object object) 171 { 172 return object == null ? PropertyNames.EMPTY_STRING : FileAccess.getPathFilename(fileAccess.getPath((String) object, null)); 173 } 174 } 175 176 /** 177 * This is a list model for the image list of the 178 * backgrounds dialog. 179 * It takes the Backgrounds config set as an argument, 180 * and "parses" it to a list of files: 181 * It goes through each image in the set, and checks it: 182 * if it is a directory it lists all image files in this directory. 183 * if it is a file, it adds the file to the list. 184 * @author rpiterman 185 */ 186 private class Model extends DefaultListModel 187 { 188 189 /** 190 * constructor. </br> 191 * see class description for a description of 192 * the handling of the given model 193 * @param model the configuration set of the background images. 194 */ Model(ConfigSet model)195 public Model(ConfigSet model) 196 { 197 try 198 { 199 for (int i = 0; i < model.getSize(); i++) 200 { 201 CGImage image = (CGImage) model.getElementAt(i); 202 String path = sd.xStringSubstitution.substituteVariables(image.cp_Href, false); 203 if (fileAccess.exists(path, false)) 204 { 205 addDir(path); 206 } 207 else 208 { 209 remove((String) model.getKey(image)); 210 } 211 } 212 } 213 catch (Exception ex) 214 { 215 ex.printStackTrace(); 216 } 217 } 218 219 /** 220 * when instanciating the model, it checks if each image 221 * exists. If it doesnot, it will be removed from 222 * the configuration. 223 * This is what this method does... 224 * @param imageName 225 */ remove(String imageName)226 private void remove(String imageName) 227 { 228 try 229 { 230 Object conf = Configuration.getConfigurationRoot(xMSF, WebWizardConst.CONFIG_PATH + "/BackgroundImages", true); 231 Configuration.removeNode(conf, imageName); 232 } 233 catch (Exception ex) 234 { 235 ex.printStackTrace(); 236 } 237 } 238 239 /** 240 * if the given url is a directory 241 * adds the images in the given directory, 242 * otherwise (if it is a file) adds the file to the list. 243 * @param url 244 */ addDir(String url)245 private void addDir(String url) 246 { 247 if (fileAccess.isDirectory(url)) 248 { 249 add(fileAccess.listFiles(url, false)); 250 } 251 else 252 { 253 add(url); 254 } 255 } 256 257 /** 258 * adds the given filenames (urls) to 259 * the list 260 * @param filenames 261 */ add(String[] filenames)262 private void add(String[] filenames) 263 { 264 for (int i = 0; i < filenames.length; i++) 265 { 266 add(filenames[i]); 267 } 268 } 269 270 /** 271 * adds the given image url to the list. 272 * if and only if it ends with jpg, jpeg or gif 273 * (case insensitive) 274 * @param filename image url. 275 */ add(String filename)276 private void add(String filename) 277 { 278 String lcase = filename.toLowerCase(); 279 if (lcase.endsWith("jpg") || 280 lcase.endsWith("jpeg") || 281 lcase.endsWith("gif")) 282 { 283 Model.this.addElement(filename); 284 } 285 } 286 } 287 } 288