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 package org.apache.openoffice.ooxml.viewer;
23 
24 import java.util.LinkedList;
25 import java.util.Queue;
26 import java.util.Set;
27 import java.util.TreeSet;
28 
29 import javax.swing.JTree;
30 import javax.swing.event.TreeSelectionEvent;
31 import javax.swing.event.TreeSelectionListener;
32 import javax.swing.tree.DefaultMutableTreeNode;
33 import javax.swing.tree.DefaultTreeModel;
34 import javax.swing.tree.TreeModel;
35 import javax.swing.tree.TreePath;
36 
37 import org.apache.openoffice.ooxml.framework.part.ContentType;
38 import org.apache.openoffice.ooxml.framework.part.IReferenceProvider;
39 import org.apache.openoffice.ooxml.framework.part.OOXMLPackage;
40 import org.apache.openoffice.ooxml.framework.part.Part;
41 import org.apache.openoffice.ooxml.framework.part.PartName;
42 
43 @SuppressWarnings("serial")
44 public class PartsView
45     extends JTree
46     implements TreeSelectionListener
47 {
PartsView( final DetailViewManager aDetailViewManager, final OOXMLPackage aPackage)48     public PartsView (
49         final DetailViewManager aDetailViewManager,
50         final OOXMLPackage aPackage)
51     {
52         maPackage = aPackage;
53         maDetailViewManager = aDetailViewManager;
54 
55         Initialize();
56 
57         getSelectionModel().addTreeSelectionListener(this);
58     }
59 
60 
61 
Initialize()62     private final void Initialize ()
63     {
64         final DefaultMutableTreeNode aRootNode = new DefaultMutableTreeNode(
65             "top level relations");
66         final TreeModel aModel = new DefaultTreeModel(aRootNode);
67 
68         final Queue<IReferenceProvider> aWorklist = new LinkedList<IReferenceProvider>();
69         aWorklist.add(maPackage);
70         final Set<PartName> aProcessedPartNames = new TreeSet<PartName>();
71         CreateChildren(aRootNode, aProcessedPartNames, maPackage);
72         setModel(aModel);
73     }
74 
75 
76 
77 
CreateChildren( final DefaultMutableTreeNode aNode, final Set<PartName> aProcessedPartNames, final IReferenceProvider aReferences)78     private void CreateChildren (
79         final DefaultMutableTreeNode aNode,
80         final Set<PartName> aProcessedPartNames,
81         final IReferenceProvider aReferences)
82     {
83         for (final PartName aTarget : aReferences.getRelatedParts().getAllTargets())
84         {
85             Part aPart = maPackage.getPart(aTarget);
86             final DefaultMutableTreeNode aRelationNode;
87             if ( ! aProcessedPartNames.contains(aPart.getPartName()))
88             {
89                 aProcessedPartNames.add(aPart.getPartName());
90                 aRelationNode = new DefaultMutableTreeNode(aPart.getPartName().GetFullname());
91                 CreateChildren(aRelationNode, aProcessedPartNames, aPart);
92             }
93             else
94             {
95                 aRelationNode = new DefaultMutableTreeNode(aPart.getPartName().GetFullname() + "...");
96             }
97             aNode.add(aRelationNode);
98         }
99     }
100 
101 
102 
103 
104     /** Callback for clicks on the part view.
105      */
106     @Override
valueChanged(final TreeSelectionEvent aEvent)107     public void valueChanged (final TreeSelectionEvent aEvent)
108     {
109         final TreePath aPath = aEvent.getNewLeadSelectionPath();
110         if (aPath != null)
111         {
112             final PartName aName= GetPackagePathForTreePath(aPath);
113             final ContentType eType = maPackage.getPart(
114                 aName).getContentType();
115             maDetailViewManager.ShowPart(
116                 aName,
117                 eType);
118         }
119     }
120 
121 
122 
123 
124 
GetPackagePathForTreePath(final TreePath aPath)125     private PartName GetPackagePathForTreePath (final TreePath aPath)
126     {
127         final PartName aName = new PartName(aPath.getLastPathComponent().toString());
128         return aName;
129     }
130 
131 
132 
133 
134 
135     private final OOXMLPackage maPackage;
136     private final DetailViewManager maDetailViewManager;
137 }
138