This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 91665
Collapse All | Expand All

(-)a/openide.loaders/src/org/openide/loaders/DataLoaderPool.java (-6 / +43 lines)
Lines 41-47 Link Here
41
41
42
package org.openide.loaders;
42
package org.openide.loaders;
43
43
44
import java.awt.Image;
44
import java.io.*;
45
import java.io.*;
46
import java.lang.reflect.Constructor;
47
import java.lang.reflect.InvocationTargetException;
45
import java.util.*;
48
import java.util.*;
46
import java.util.logging.Level;
49
import java.util.logging.Level;
47
import java.util.logging.Logger;
50
import java.util.logging.Logger;
Lines 476-482 implements java.io.Serializable { Link Here
476
        Enumeration<? extends DataObject.Factory> en = allLoaders (fo);
479
        Enumeration<? extends DataObject.Factory> en = allLoaders (fo);
477
        while (en.hasMoreElements ()) {
480
        while (en.hasMoreElements ()) {
478
            DataObject.Factory l = en.nextElement ();
481
            DataObject.Factory l = en.nextElement ();
479
            DataObject obj = l.findDataObject (fo, recognized);
482
            DataObject obj;
483
            if (l instanceof DataLoader) {
484
                obj = l.findDataObject (fo, recognized);
485
            } else {
486
                obj = DataObjectPool.handleFindDataObject(l, fo, recognized);
487
            }
480
            if (!recognized.isEmpty()) {
488
            if (!recognized.isEmpty()) {
481
                for (FileObject f : recognized) {
489
                for (FileObject f : recognized) {
482
                    r.markRecognized(f);
490
                    r.markRecognized(f);
Lines 575-581 implements java.io.Serializable { Link Here
575
        }
583
        }
576
        return null;
584
        return null;
577
    }
585
    }
578
    
586
587
    /** Factory method to create default implementation of a factory for
588
     * data objects. It takes the class of the <code>DataObject</code> and
589
     * is ready to call its constructor. The constructor needs to take two
590
     * arguments: FileObject and MultiDataLoader. It can throw IOException as
591
     * is usual among DataObject constructors.
592
     * <p>
593
     * You can also invoke this method from a layer by following definition:
594
     * <pre>
595
     * &lt;file name="nameofyourfile.instance"&gt;
596
     *   &attr name="instanceCreate" methodvalue="org.openide.loaders.DataLoaderPool.factory"/&gt;
597
     *   &attr name="dataObjectClass" stringvalue="org.your.pkg.YourDataObject"/&gt;
598
     *   &attr name="mimeType" stringvalue="yourmime/type"/&gt;
599
     *   &attr name="SystemFileSystem.localizingIcon" stringvalue="org/your/pkg/YourDataObject.png"/&gt;
600
     * &lt;/file&gt;
601
     * </pre>
602
     * @param clazz the class of the data object to create. Must have appropriate
603
     *    constructor.
604
     * @param mimeType the mime type associated with the object, used for 
605
     *    example to create the right actions for the object's node
606
     * @return factory to be registered in <code>Loaders/mime/type/Factories</code>
607
     *    in layer file
608
     * @since 7.1
609
     */
610
    public static <T extends DataObject> DataObject.Factory factory(
611
        Class<T> dataObjectClass, String mimeType, Image image
612
    ) {
613
        return new MimeFactory<T>(dataObjectClass, mimeType, image, null);
614
    }
615
    static <T extends DataObject> DataObject.Factory factory(
616
        FileObject fo
617
    ) throws ClassNotFoundException {
618
        return MimeFactory.layer(fo);
619
    }
579
    
620
    
580
    /** Lazy getter for system loaders.
621
    /** Lazy getter for system loaders.
581
     */
622
     */
Lines 801-811 private static class InstanceLoader exte Link Here
801
        };
842
        };
802
    }
843
    }
803
} // end of InstanceLoader
844
} // end of InstanceLoader
804
805
806
807
    
845
    
808
809
/** Loader for file objects not recognized by any other loader */
846
/** Loader for file objects not recognized by any other loader */
810
private static final class DefaultLoader extends MultiFileLoader {
847
private static final class DefaultLoader extends MultiFileLoader {
811
    static final long serialVersionUID =-6761887227412396555L;
848
    static final long serialVersionUID =-6761887227412396555L;
(-)a/openide.loaders/src/org/openide/loaders/DataNode.java (-2 / +20 lines)
Lines 246-251 public class DataNode extends AbstractNo Link Here
246
         }
246
         }
247
         return super.getHtmlDisplayName();
247
         return super.getHtmlDisplayName();
248
     }    
248
     }    
249
     
250
     private java.awt.Image getImageFromFactory() {
251
         MimeFactory<?> fact = getLookup().lookup(MimeFactory.class);
252
         return fact != null ? fact.getImage() : null;
253
     }
249
254
250
    /** Get the displayed icon for this node.
255
    /** Get the displayed icon for this node.
251
     * A filesystem may {@link org.openide.filesystems.FileSystem#getStatus specially alter} this.
256
     * A filesystem may {@link org.openide.filesystems.FileSystem#getStatus specially alter} this.
Lines 254-261 public class DataNode extends AbstractNo Link Here
254
     * @param type the icon type from {@link java.beans.BeanInfo}
259
     * @param type the icon type from {@link java.beans.BeanInfo}
255
     * @return the desired icon
260
     * @return the desired icon
256
    */
261
    */
262
    @Override
257
    public java.awt.Image getIcon (int type) {
263
    public java.awt.Image getIcon (int type) {
258
        java.awt.Image img = super.getIcon (type);
264
        java.awt.Image img = getImageFromFactory();
265
        if (img == null) {
266
            img = super.getIcon (type);
267
        }
259
268
260
        try {
269
        try {
261
            img = obj.getPrimaryFile ().getFileSystem ().getStatus ().annotateIcon (img, type, new LazyFilesSet());
270
            img = obj.getPrimaryFile ().getFileSystem ().getStatus ().annotateIcon (img, type, new LazyFilesSet());
Lines 273-280 public class DataNode extends AbstractNo Link Here
273
    * @param type the icon type from {@link java.beans.BeanInfo}
282
    * @param type the icon type from {@link java.beans.BeanInfo}
274
    * @return the desired icon
283
    * @return the desired icon
275
    */
284
    */
285
    @Override
276
    public java.awt.Image getOpenedIcon (int type) {
286
    public java.awt.Image getOpenedIcon (int type) {
277
        java.awt.Image img = super.getOpenedIcon(type);
287
        java.awt.Image img = getImageFromFactory();
288
        if (img == null) {
289
            img = super.getOpenedIcon(type);
290
        }
278
291
279
        try {
292
        try {
280
            img = obj.getPrimaryFile ().getFileSystem ().getStatus ().annotateIcon (img, type, new LazyFilesSet());
293
            img = obj.getPrimaryFile ().getFileSystem ().getStatus ().annotateIcon (img, type, new LazyFilesSet());
Lines 351-356 public class DataNode extends AbstractNo Link Here
351
364
352
        if (systemActions != null) {
365
        if (systemActions != null) {
353
            return systemActions;
366
            return systemActions;
367
        }
368
369
        MimeFactory<?> mime = getLookup().lookup(MimeFactory.class);
370
        if (mime != null) {
371
            return mime.getActions();
354
        }
372
        }
355
373
356
        return obj.getLoader ().getSwingActions ();
374
        return obj.getLoader ().getSwingActions ();
(-)a/openide.loaders/src/org/openide/loaders/DataObjectPool.java (+20 lines)
Lines 156-161 implements ChangeListener { Link Here
156
            getPOOL ().enterRecognition(fo);
156
            getPOOL ().enterRecognition(fo);
157
            
157
            
158
            ret = loader.handleFindDataObject (fo, rec);
158
            ret = loader.handleFindDataObject (fo, rec);
159
        } finally {
160
            exitAllowConstructor (prev);
161
        }
162
        
163
        return ret;
164
    }
165
    
166
    /** Calls into one loader. Setups security condition to allow DataObject ocnstructor
167
     * to succeed.
168
     */
169
    public static DataObject handleFindDataObject (DataObject.Factory factory, FileObject fo, Set<? super FileObject> rec) 
170
    throws java.io.IOException {
171
        DataObject ret;
172
        
173
        Collection<Item> prev = enterAllowContructor();
174
        try {
175
            // make sure this thread is allowed to recognize
176
            getPOOL ().enterRecognition(fo);
177
            
178
            ret = factory.findDataObject (fo, rec);
159
        } finally {
179
        } finally {
160
            exitAllowConstructor (prev);
180
            exitAllowConstructor (prev);
161
        }
181
        }
(-)a/openide.loaders/test/unit/src/org/openide/loaders/DataLoaderInLayerTest.java (+51 lines)
Lines 42-53 package org.openide.loaders; Link Here
42
package org.openide.loaders;
42
package org.openide.loaders;
43
43
44
44
45
import java.awt.Image;
46
import java.awt.Toolkit;
45
import org.openide.filesystems.*;
47
import org.openide.filesystems.*;
46
import java.io.IOException;
48
import java.io.IOException;
47
import java.util.*;
49
import java.util.*;
48
import org.netbeans.junit.*;
50
import org.netbeans.junit.*;
49
import java.beans.PropertyChangeListener;
51
import java.beans.PropertyChangeListener;
52
import java.net.URL;
53
import javax.swing.Action;
50
import junit.framework.Test;
54
import junit.framework.Test;
55
import org.openide.actions.EditAction;
56
import org.openide.nodes.Node;
51
57
52
/** Check what can be done when registering loaders in layer.
58
/** Check what can be done when registering loaders in layer.
53
 * @author Jaroslav Tulach
59
 * @author Jaroslav Tulach
Lines 93-98 public class DataLoaderInLayerTest exten Link Here
93
            }
99
            }
94
        }
100
        }
95
    }
101
    }
102
    private static <F extends DataObject.Factory> void addRemove(String mime, F factory, boolean add) throws IOException {
103
        String res = "Loaders/" + mime + "/Factories/" + factory.getClass().getSimpleName().replace('.', '-') + ".instance";
104
        FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
105
        if (add) {
106
            FileObject fo = FileUtil.createData(root, res);
107
            fo.setAttribute("instanceCreate", factory);
108
            assertSame("No serialization, just memory fs is used", factory, fo.getAttribute("instanceCreate"));
109
        } else {
110
            FileObject fo = root.getFileObject(res);
111
            if (fo != null) {
112
                fo.delete();
113
            }
114
        }
115
    }
96
    
116
    
97
    public void testSimpleGetChildren() throws Exception {
117
    public void testSimpleGetChildren() throws Exception {
98
        DataLoader l = DataLoader.getLoader(SimpleUniFileLoader.class);
118
        DataLoader l = DataLoader.getLoader(SimpleUniFileLoader.class);
Lines 122-127 public class DataLoaderInLayerTest exten Link Here
122
            assertEquals(SimpleDataObject.class, dob.getClass());
142
            assertEquals(SimpleDataObject.class, dob.getClass());
123
        } finally {
143
        } finally {
124
            addRemove("text/plain", SimpleFactory.class, false);
144
            addRemove("text/plain", SimpleFactory.class, false);
145
        }
146
    }
147
148
    public void testFactoryInstanceRegistrationWorksAsWell() throws Exception {
149
        URL u = DataLoaderInLayerTest.class.getResource("/org/openide/loaders/saveAll.gif");
150
        Image img = Toolkit.getDefaultToolkit().createImage(u);
151
        
152
        DataObject.Factory f = DataLoaderPool.factory(SimpleDataObject.class, "text/simplefactory", img);
153
        
154
        addRemove("text/plain", f, true);
155
        try {
156
            FileSystem lfs = createFS("folderF/file.simple");
157
            FileObject fo = lfs.findResource("folderF");
158
            DataFolder df = DataFolder.findFolder(fo);
159
            DataObject[] arr = df.getChildren();
160
            assertEquals("One object", 1, arr.length);
161
            DataObject dob = arr[0];
162
            assertEquals(SimpleDataObject.class, dob.getClass());
163
            
164
            FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
165
            FileObject edit = FileUtil.createData(root, "/Loaders/text/simplefactory/Actions/org-openide-actions-EditAction.instance");
166
            
167
            Node node = dob.getNodeDelegate();
168
            Action[] actions = node.getActions(true);
169
            assertEquals("One action is present: " + Arrays.asList(actions), 1, actions.length);
170
            assertEquals("It is the edit one", EditAction.class, actions[0].getClass());
171
            
172
            assertSame("Icon is propagated for open", img, node.getOpenedIcon(0));
173
            assertSame("Icon is propagated", img, node.getIcon(0));
174
        } finally {
175
            addRemove("text/plain", f, false);
125
        }
176
        }
126
    }
177
    }
127
178

Return to bug 91665