Normand Briere
2019-09-15 d5fac992bb6a91e52a90e8318060f5f26ddab982
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
 
import java.awt.dnd.*;
import java.awt.datatransfer.*;
 
import java.io.File;
 
import grafeme.ui.*;
 
public class cFileSystemPane extends cGridBag implements ActionListener, ItemListener, DragGestureListener,
        //DragSourceListener,
        DropTargetListener
{
    cTree jTree = new cTree();
    iCallBack owner;
    
    JButton refreshButton;
    JButton rootButton;
    JButton loadButton;
    JButton printButton;
    JButton replaceButton;
    JCheckBox normalsCB;
    JCheckBox trimCB;
    JCheckBox stripCB;
    JCheckBox genUVCB;
 
    cCheckBox mergeAttributesCB;
    
    cFileSystemPane(iCallBack o)
    {
        owner = o;
        
        setName("Files");
        //System.out.println("layout : " + getLayout());
        //setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        //setLayout(new GridBagLayout());
        //ToolTipManager.sharedInstance().registerComponent(jTree);
        jTree.setCellRenderer(new cFileSystemModel.Renderer());
        
        ResetModel(true);
 
        JScrollPane tree = new JScrollPane(jTree);
        //jTree.addTreeSelectionListener(this);
        //jTree.setRootVisible(false);
        //jTree.setEditable(true);
        jTree.setDragEnabled(true);
 
        /*
        JPanel jp = new JPanel(new GridBagLayout());
        //System.out.println("layout : " + jp.getPreferredSize());
        jp.add(refreshButton = new JButton("Refresh"));
        jp.add(refreshButton = new JButton("Refresh"));
        jp.add(refreshButton = new JButton("Refresh"));
        //jp.add(refreshButton = new JButton("Refresh"));
        //jp.add(refreshButton = new JButton("Refresh"));
        //jp.setSize(new Dimension(20,20));
         */
   GridBagConstraints aConstraints = new GridBagConstraints(0,0,1,1, 1.0,1.0,
            GridBagConstraints.EAST,GridBagConstraints.HORIZONTAL,new Insets(1,1,1,1), 0,0);
 
        cGridBag fileCommands = new cGridBag();
        
        fileCommands.add(loadButton = new JButton("Load")); //, aConstraints);
                loadButton.setToolTipText("Load selected file(s)");
                loadButton.addActionListener(this);
        fileCommands.add(refreshButton = new JButton("User")); //, aConstraints);
                refreshButton.setToolTipText("Refresh user tree");
                refreshButton.addActionListener(this);
        fileCommands.add(rootButton = new JButton("Root")); //, aConstraints);
                rootButton.setToolTipText("Refresh root tree");
                rootButton.addActionListener(this);
        
        if (Globals.ADVANCED)
        {
                fileCommands.add(printButton = new JButton("Print")); //, aConstraints);
                printButton.setToolTipText("Print file path in terminal");
                printButton.addActionListener(this);
                fileCommands.add(mergeAttributesCB = new cCheckBox("Merge", cJME.mergeAttributes)); //, aConstraints);
                mergeAttributesCB.setToolTipText("Merge common attributes (recommended)");
                mergeAttributesCB.addItemListener(this);
//        replaceButton.addActionListener(this);
   //     add(replaceButton = new JButton("Replace"), aConstraints);
        }
 
//        aConstraints.gridx += 1;
//            add(trimCB = new cCheckBox("Trim", cJME.trim), aConstraints);
//            trimCB.addItemListener(this);
//        aConstraints.gridx += 1;
//            add(normalsCB = new cCheckBox("Normal", cJME.gennormals), aConstraints);
//            normalsCB.addItemListener(this);
//        aConstraints.gridx += 1;
//            add(stripCB = new cCheckBox("Strip", cJME.stripify), aConstraints);
//            stripCB.addItemListener(this);
//        aConstraints.gridx += 1;
//            add(genUVCB = new cCheckBox("UV", cJME.genUV), aConstraints);
//            genUVCB.addItemListener(this);
                        
        add(fileCommands);
        fileCommands.preferredHeight = 1;
        
        Return();
        
        cGridBag treeBag = new cGridBag();
        
        treeBag.add(tree);
        
        treeBag.preferredHeight = 30;
        
        add(treeBag); //, aConstraints);
        
            GroupEditor oe = (GroupEditor) owner;
 
//        DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer(oe.cameraView, DnDConstants.ACTION_COPY_OR_MOVE, this); // ACTION_LINK ??
//            /*DropTarget dropTarget =*/ new DropTarget(oe.cameraView, this);
    }
 
    void ResetModel(boolean user)
    {
        cFilter filter = new cFilter();
        
        filter.add("3ds");
        filter.add("dae");
        filter.add("obj");
        filter.add("gfd");
        filter.add("bvh");
        filter.add("wrl");
        filter.add("vrml");
        filter.add("x3d");
        filter.add("jpg");
        filter.add("jpeg");
        filter.add("gif");
        filter.add("png");
        filter.add("tre");
        filter.add("tga");
        filter.add("bmp"); // not supported
        
        File root = java.io.File.listRoots()[0];
        File defaultDirectory = javax.swing.filechooser.FileSystemView.getFileSystemView().getDefaultDirectory();
        
        jTree.setModel(new cFileSystemModel(
                user?defaultDirectory:root,
                filter
        ));
    }
    
    public void itemStateChanged(ItemEvent e)
    {
        if(e.getSource() == normalsCB)
            cJME.gennormals = (e.getStateChange() == ItemEvent.SELECTED);
        else
        if(e.getSource() == trimCB)
            cJME.trim = (e.getStateChange() == ItemEvent.SELECTED);
        else
        if(e.getSource() == stripCB)
            cJME.stripify = (e.getStateChange() == ItemEvent.SELECTED);
        else
        if(e.getSource() == genUVCB)
            cJME.genUV = (e.getStateChange() == ItemEvent.SELECTED);
        else
        if(e.getSource() == mergeAttributesCB)
            cJME.mergeAttributes = (e.getStateChange() == ItemEvent.SELECTED);
        else
            assert(false);
        
        //preserveCB.setEnabled(cJME.gennormals || cJME.stripify);
    }
 
    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == refreshButton)
        {
            ResetModel(true);
            return;
        }
        
        if(event.getSource() == rootButton)
        {
            ResetModel(false);
            return;
        }
        
        if(event.getSource() == loadButton)
        {
            TreePath[] paths = jTree.getSelectionPaths();
            if (paths == null) // || (path.getPathCount() <= 1))
              // We can't move the root node or an empty selection
              return;
            File[] files = new File[paths.length];
 
            for(int i=0; i<paths.length; i++)
                files[i] = (File) paths[i].getLastPathComponent();
 
            owner.Callback(files);
        }
        
        if(event.getSource() == printButton)
        {
            TreePath[] paths = jTree.getSelectionPaths();
            if (paths == null) // || (path.getPathCount() <= 1))
              // We can't move the root node or an empty selection
              return;
            
            for(int i=0; i<paths.length; i++)
            {
                File file = (File) paths[i].getLastPathComponent();
                
                //System.out.println("FILE");
                System.err.println(file);
            }
        }
    }
    
    public void dragGestureRecognized(DragGestureEvent dge)
    {
            System.out.println("dragGestureRecognized (FileSystem) : " + dge);
 
            // Patch for DnD failure (unselect first)
            owner.DragGesture();
            
            /**/
            //DragSource.getDefaultDragSource().startDrag(dge, DragSource.DefaultMoveNoDrop, new StringSelection("Allo"), this);
            TreePath[] path = jTree.getSelectionPaths();
            if (path == null) // || (path.length <= 1))
              // We can't move the root node or an empty selection
              return;
            File[] file = new File[path.length];
            
            for(int i=0; i<path.length; i++)
                file[i] = (File) path[i].getLastPathComponent();
            TransferableFile transferable = new TransferableFile(file);
//            boolean success;
//            do
//            {
//                success = false;
                //try
                //{
            System.out.println("startDrag (FileSystem) : " + dge);
                    DragSource.getDefaultDragSource().startDrag(dge, DragSource.DefaultMoveDrop, transferable, null); // this);
//                    success = true;
                //}catch(Exception e) {}
//            } while(!success);
            /**/
    }
 
   public void    drop(DropTargetDropEvent dtde) 
    //      The drag operation has terminated with a drop on this DropTarget.
   {
            GroupEditor objEditor = (GroupEditor) owner;
            
                objEditor.location = dtde.getLocation();
       Component target = dtde.getDropTargetContext().getDropTarget().getComponent();
 
                    System.out.println("drop2 = " + dtde + "; drop : " + target);
                    
                //assert dropTarget == dtde.getSource();
                if (target instanceof iCameraPane)
                {
                    Object object = null;
                    try
                    {
                        object = dtde.getTransferable().getTransferData(
                            dtde.getTransferable().getTransferDataFlavors()[0]);
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                    System.out.println("Transfer2 = " + object + "; drop : " + target);
                    
                    owner.Callback(object);
                }
        }
 
   public void    dragEnter(DropTargetDragEvent dtde)
   //       Called when a drag operation has encountered the DropTarget.
   {
       System.out.println("dragEnter2 : " + dtde);
   }
   public void    dragExit(DropTargetEvent dte) 
    //      The drag operation has departed the DropTarget without dropping.
   {
       System.out.println("dragExit2 : " + dte);
   }
   public void    dragOver(DropTargetDragEvent dtde) 
    //      Called when a drag operation is ongoing on the DropTarget.
   {
       //System.out.println("dragOver : " + dtde.getSource());
   }
        
   public void    dropActionChanged(DropTargetDragEvent dtde) 
   // Called if the user has modified the current drop gesture
   {
       System.out.println("dropActionChanged2 : " + dtde);
   }
 
//        
//    public void    dragDropEnd(DragSourceDropEvent dsde) 
//    //          This method is invoked to signify that the Drag and Drop operation is complete.
//    {
//        System.out.println("dragDropEnd : " + dsde.getSource());
//        /*
//    if (//dsde.getDropSuccess() &&
//        true) //(dsde.getDropAction() == DnDConstants.ACTION_MOVE))
//    {
//      //((DefaultTreeModel) jTree.getModel()).removeNodeFromParent(oldNode);
//      loadClipboard(true);
//    }
//    */
//    }
//    public void    dragEnter(DragSourceDragEvent dsde) 
//    //          Called as the hotspot enters a platform dependent drop site.
//    {
//        System.out.println("dragEnter : " + dsde);
//    }
//    public void    dragExit(DragSourceEvent dse) 
//    //          Called as the hotspot exits a platform dependent drop site.
//    {
//        System.out.println("dragExit : " + dse);
//    }
//    public void    dragOver(DragSourceDragEvent dsde) 
//    //          Called as the hotspot moves over a platform dependent drop site.
//    {
//        //System.out.println("dragOver : " + dsde);
//    }
//    public void    dropActionChanged(DragSourceDragEvent dsde) 
//    //          Called when the user has modified the drop gesture.
//    {
//        System.out.println("dropActionChanged : " + dsde);
//    System.out.println("Action: " + dsde.getDropAction());
//    System.out.println("Target Action: " + dsde.getTargetActions());
//    System.out.println("User Action: " + dsde.getUserAction());
//    }
   
    public static DataFlavor FILE_FLAVOR = new DataFlavor(File[].class,
            "File");
 
    static class TransferableFile implements Transferable {
 
        DataFlavor flavors[] = {FILE_FLAVOR};
        File[] file;
 
        public TransferableFile(File[] f) {
            file = f;
        }
 
        public synchronized DataFlavor[] getTransferDataFlavors() {
            return flavors;
        }
 
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return (flavor.getRepresentationClass() == File[].class);
        }
 
        public synchronized Object getTransferData(DataFlavor flavor)
                throws UnsupportedFlavorException, java.io.IOException {
            if (isDataFlavorSupported(flavor)) {
                return file;
            } else {
                throw new UnsupportedFlavorException(flavor);
            }
        }
    }
}