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