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 24 package org.openoffice.netbeans.modules.office.nodes; 25 26 import java.io.IOException; 27 import java.util.*; 28 import javax.swing.event.ChangeEvent; 29 import javax.swing.event.ChangeListener; 30 31 import org.openide.nodes.*; 32 import org.openide.actions.*; 33 import org.openide.util.actions.SystemAction; 34 import org.openide.util.HelpCtx; 35 36 import org.openoffice.netbeans.modules.office.options.OfficeSettings; 37 import org.openoffice.netbeans.modules.office.utils.NagDialog; 38 import org.openoffice.netbeans.modules.office.actions.OfficeDocumentCookie; 39 40 public class OfficeDocumentChildren extends Children.Keys 41 implements ChangeListener { 42 43 private OfficeDocumentCookie document = null; 44 OfficeDocumentChildren(OfficeDocumentCookie cookie)45 public OfficeDocumentChildren(OfficeDocumentCookie cookie) { 46 document = cookie; 47 } 48 refreshKeys()49 private void refreshKeys() { 50 if (document == null) { 51 setKeys(Collections.EMPTY_SET); 52 return; 53 } 54 55 Enumeration parcels = document.getParcels(); 56 if (parcels.hasMoreElements() != true) { 57 setKeys(Collections.EMPTY_SET); 58 return; 59 } 60 61 ArrayList keys = new ArrayList(); 62 while (parcels.hasMoreElements()) { 63 String parcel = (String)parcels.nextElement(); 64 keys.add(parcel); 65 } 66 setKeys(keys); 67 } 68 addNotify()69 protected void addNotify() { 70 super.addNotify(); 71 document.addChangeListener(this); 72 refreshKeys(); 73 } 74 removeNotify()75 protected void removeNotify() { 76 super.removeNotify(); 77 document.removeChangeListener(this); 78 setKeys(Collections.EMPTY_SET); 79 } 80 createNodes(Object key)81 protected Node[] createNodes(Object key) { 82 String name = (String)key; 83 return new Node[] {new ParcelNode(name)}; 84 } 85 stateChanged(ChangeEvent e)86 public void stateChanged(ChangeEvent e) { 87 refreshKeys(); 88 } 89 90 private class ParcelNode extends AbstractNode { 91 private String name; 92 ParcelNode(String name)93 public ParcelNode(String name) { 94 super(Children.LEAF); 95 this.name = name; 96 init(); 97 } 98 init()99 private void init() { 100 setIconBase("/org/openoffice/netbeans/modules/office/resources/ParcelIcon"); 101 102 setName(name); 103 setDisplayName(name.substring(name.lastIndexOf("/") + 1)); 104 setShortDescription(name); 105 } 106 createActions()107 protected SystemAction[] createActions() { 108 return new SystemAction[] { 109 SystemAction.get(DeleteAction.class), 110 }; 111 } 112 getHelpCtx()113 public HelpCtx getHelpCtx() { 114 return HelpCtx.DEFAULT_HELP; 115 } 116 canDestroy()117 public boolean canDestroy() { 118 return true; 119 } 120 destroy()121 public void destroy() throws IOException { 122 OfficeSettings settings = OfficeSettings.getDefault(); 123 String message = "If you already have this document open in " + 124 "Office, please close it before continuing. Click OK to " + 125 "delete this parcel."; 126 127 if (settings.getWarnBeforeParcelDelete() == true) { 128 NagDialog warning = NagDialog.createConfirmationDialog( 129 message, "Show this message in future", true); 130 131 boolean result = warning.show(); 132 133 if (warning.getState() == false) 134 settings.setWarnBeforeParcelDelete(false); 135 136 if (result == false) 137 return; 138 } 139 super.destroy(); 140 document.removeParcel(name); 141 } 142 } 143 } 144