Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
   * Other names may be trademarks of their respective owners.
   *
   * The contents of this file are subject to the terms of either the GNU
  * General Public License Version 2 only ("GPL") or the Common
  * Development and Distribution License("CDDL") (collectively, the
  * "License"). You may not use this file except in compliance with the
  * License. You can obtain a copy of the License at
  * http://www.netbeans.org/cddl-gplv2.html
  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  * specific language governing permissions and limitations under the
  * License.  When distributing the software, include this License Header
  * Notice in each file and include the License file at
  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
  * particular file as subject to the "Classpath" exception as provided
  * by Oracle in the GPL Version 2 section of the License file that
  * accompanied this code. If applicable, add the following below the
  * License Header, with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  * 
  * If you wish your version of this file to be governed by only the CDDL
  * or only the GPL Version 2, indicate your decision by adding
  * "[Contributor] elects to include this software in this distribution
  * under the [CDDL or GPL Version 2] license." If you do not indicate a
  * single choice of license, a recipient has the option to distribute
  * your version of this file under either the CDDL, the GPL Version 2 or
  * to extend the choice of license to its licensees as provided above.
  * However, if you add GPL Version 2 code and therefore, elected the GPL
  * Version 2 license, then the option applies only if the new code is
  * made subject to such option by the copyright holder.
  * 
  * Contributor(s):
  * 
  * Portions Copyrighted 2008 Sun Microsystems, Inc.
  */
 
 package org.netbeans.modules.ide.ergonomics.fod;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;

Author(s):
Jaroslav Tulach <jtulach@netbeans.org>, Jirka Rechtacek <jrechtacek@netbeans.org>
 
@ServiceProvider(service=ProjectFactory.class, position=30000)
    static final Logger LOG = Logger.getLogger("org.netbeans.modules.ide.ergonomics.projects"); // NOI18N
    public FeatureProjectFactory() {
        OpenProjects.getDefault().addPropertyChangeListener(this);
    }
    final static class Data {
        private final boolean deepCheck;
        private final FileObject dir;
        private Map<String,Stringdata;
        private Map<String,Documentdoms;
        public Data(FileObject dirboolean deepCheck) {
            this. = deepCheck;
            this. = dir;
        }
        Document dom(String relative) {
            Document doc =  == null ? null : .get(relative);
            if (doc != null) {
                return doc;
            }
            FileObject fo = .getFileObject(relative);
            if (fo == null) {
                return null;
            }
            File f = FileUtil.toFile(fo);
            try {
                DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                if (f != null) {
                    doc = b.parse(f);
                } else {
                    InputStream is = fo.getInputStream();
                    doc = b.parse(is);
                }
                if ( == null) {
                     = new HashMap<String,Document>();
                }
                .put(relativedoc);
                return doc;
            } catch (ParserConfigurationException parserConfigurationException) {
                .log(."Cannot configure XML parser"parserConfigurationException); // NOI18N
            } catch (SAXException sAXException) {
                .log(."XML broken in " + fsAXException); // NOI18N
            } catch (IOException iOException) {
                .log(."Cannot read " + fiOException); // NOI18N
            }
            return null;
        }
        final boolean hasFile(String relative) {
            return .getFileObject(relative) != null;
        }
        final boolean isDeepCheck() {
            return ;
        }
        @Override
        public String toString() {
            return .getPath();
        }
        final synchronized String is(String relative) {
            FileObject prj = .getFileObject(relative);
            if (prj == null) {
                return null;
            }
            String content =  == null ? null : .get(relative);
            if (content != null) {
                return content;
            }
            byte[] arr = new byte[4000];
            int len;
            InputStream is = null;
            try {
                is = prj.getInputStream();
                len = is.read(arr);
                if (len >= 0) {
                    content = new String(arr, 0, len"UTF-8");
                }
            } catch (IOException ex) {
                .log(."exception while reading " + prjex); // NOI18N
                len = -1;
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException ex) {
                        Exceptions.printStackTrace(ex);
                    }
                }
            }
            .log(."    read {0} bytes"len); // NOI18N
            if (len == -1) {
                return null;
            }
            if ( == null) {
                 = new HashMap<String,String>();
            }
            .put(relativecontent);
            return content;
        }
    }
    public boolean isProject(FileObject projectDirectory) {
        Data d = new Data(projectDirectoryfalse);
        for (FeatureInfo info : FeatureManager.features()) {
            if (!info.isPresent()) {
                continue;
            }
            if (!info.isEnabled() && (info.isProject(d) == 1)) {
                return true;
            }
        }
        return false;
    }
    public Project loadProject(FileObject projectDirectoryProjectState statethrows IOException {
        Data d = new Data(projectDirectorytrue);
        
        FeatureInfo lead = null;
        List<FeatureInfoadditional = new ArrayList<FeatureInfo>();
        int notEnabled = 0;
        for (FeatureInfo info : FeatureManager.features()) {
            if (!info.isPresent()) {
                continue;
            }
            switch (info.isProject(d)) {
                case 0: break;
                case 1:
                    lead = info;
                    if (!info.isEnabled()) {
                        notEnabled++;
                    }
                break;
                case 2:
                    additional.add(info);
                    if (!info.isEnabled()) {
                        notEnabled++;
                    }
                    break;
                defaultassert false;
            }
        }
        if (lead == null || notEnabled == 0) {
            return null;
        }
        return new FeatureNonProject(projectDirectoryleadstateadditional);
    }
    @Override
    public void saveProject(Project projectthrows IOExceptionClassCastException {
    }
    @Override
    public void run() {
        final List<FeatureInfoadditional = new ArrayList<FeatureInfo>();
        FeatureInfo f = null;
        for (Project p : OpenProjects.getDefault().getOpenProjects()) {
            Data d = new Data(p.getProjectDirectory(), true);
            for (FeatureInfo info : FeatureManager.features()) {
                switch (info.isProject(d)) {
                    case 0:
                        break;
                    case 1:
                        f = info;
                        break;
                    case 2:
                        f = info;
                        additional.add(info);
                        break;
                    default:
                        assert false;
                }
            }
        }
        if (f != null && !additional.isEmpty()) {
            final FeatureInfo finalF = f;
            final FeatureInfo[] addF = additional.toArray(new FeatureInfo[0]);
            FeatureManager.logUI("ERGO_PROJECT_OPEN"finalF.clusterName);
            FindComponentModules findModules = new FindComponentModules(finalFaddF);
            Collection<UpdateElementtoEnable = findModules.getModulesForEnable();
            if (toEnable != null && !toEnable.isEmpty()) {
                ModulesActivator enabler = new ModulesActivator(toEnablefindModules);
                enabler.getEnableTask().waitFinished();
            }
        }
    }
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
            RequestProcessor.Task t = FeatureManager.getInstance().create(this);
            t.schedule(0);
        }
    }
    private static final class FeatureNonProject 
    implements ProjectChangeListener {
        private final FeatureDelegate delegate;
        private final FeatureInfo info;
        private final FeatureInfo[] additional;
        private final Lookup lookup;
        private ProjectState state;
        private String error;
        private final ChangeListener weakL;
        public FeatureNonProject(
            FileObject dirFeatureInfo info,
            ProjectState stateList<FeatureInfoadditional
        ) {
            this. = new FeatureDelegate(dirthis);
            this. = info;
            this. = additional.toArray(new FeatureInfo[0]);
            this. = Lookups.proxy();
            this. = state;
            this. = WeakListeners.change(this, FeatureManager.getInstance());
            FeatureManager.getInstance().addChangeListener();
        }
        
        public FileObject getProjectDirectory() {
            return .;
        }
        public Lookup getLookup() {
            return ;
        }
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Project) {
                return ((Project)obj).getProjectDirectory().equals(getProjectDirectory());
            }
            return false;
        }
        @Override
        public int hashCode() {
            return getProjectDirectory().hashCode();
        }
        public void stateChanged(ChangeEvent e) {
            if (.isEnabled()) {
                switchToReal();
            }
        }
        final void switchToReal() {
            ProjectState s = null;
            synchronized (this) {
                s = ;
                 = null;
            }
            if (s != null) {
                try {
                    s.notifyDeleted();
                    Project p = ProjectManager.getDefault().findProject(getProjectDirectory());
                    if (p == FeatureNonProject.this) {
                        throw new IllegalStateException("New project shall be found! " + p); // NOI18N
                    }
                    .associate(p);
                } catch (IOException ex) {
                     = ex.getLocalizedMessage();
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
        private final class FeatureOpenHook extends ProjectOpenedHook
        implements RunnableProgressMonitor {
            @Override
            protected void projectOpened() {
                if ( == null) {
                    return;
                }
                RequestProcessor.Task t = FeatureManager.getInstance().create(this);
                t.schedule(0);
                t.waitFinished ();
                if ( == null) {
                    switchToReal();
                    // make sure support for projects we depend on are also enabled
                    SubprojectProvider sp = getLookup().lookup(SubprojectProvider.class);
                    if (sp != null) {
                        for (Project subP : sp.getSubprojects()) {
                            FeatureNonProject toOpen;
                            toOpen = subP.getLookup().lookup(FeatureNonProject.class);
                            if (toOpen != null) {
                                toOpen.delegate.hook.projectOpened();
                            }
                        }
                    }
                } else {
                    .associate(new BrokenProject(getProjectDirectory(), ));
                }
            }
            @Override
            protected void projectClosed() {
            }
            public void run() {
                FeatureManager.logUI("ERGO_PROJECT_OPEN".);
                 = null;
                FindComponentModules findModules = new FindComponentModules();
                Collection<UpdateElementtoInstall = findModules.getModulesForInstall ();
                Collection<UpdateElementtoEnable = findModules.getModulesForEnable ();
                if (toInstall != null && ! toInstall.isEmpty ()) {
                    ModulesInstaller installer = new ModulesInstaller(toInstallfindModulesthis);
                    installer.getInstallTask ().waitFinished ();
                } else if (toEnable != null && ! toEnable.isEmpty ()) {
                    ModulesActivator enabler = new ModulesActivator (toEnablefindModulesthis);
                    enabler.getEnableTask ().waitFinished ();
                }
            }
            public void onDownload(ProgressHandle progressHandle) {
            }
            public void onValidate(ProgressHandle progressHandle) {
            }
            public void onInstall(ProgressHandle progressHandle) {
            }
            public void onEnable(ProgressHandle progressHandle) {
            }
            public void onError(String message) {
                 = message;
            }
        } // end of FeatureOpenHook
    } // end of FeatureNonProject
    private static final class FeatureDelegate 
        private final FileObject dir;
        private final PropertyChangeSupport support;
        Lookup delegate;
        private final InstanceContent ic = new InstanceContent();
        private final Lookup hooks = new AbstractLookup();
        private final FeatureNonProject.FeatureOpenHook hook;
        private List<RootNodelvs;
        public FeatureDelegate(FileObject dirFeatureNonProject feature) {
            this. = dir;
            this. = feature.new FeatureOpenHook();
            .add(UILookupMergerSupport.createProjectOpenHookMerger());
            this. = new ProxyLookup(
                Lookups.fixed(featurethis),
                LookupProviderSupport.createCompositeLookup(
                    "../nonsence" // NOI18N
                )
            );
            this. = new PropertyChangeSupport(this);
        }
        public Lookup getLookup() {
            return ;
        }
        public String getName() {
            ProjectInformation info = .lookup(ProjectInformation.class);
            if (info != this) {
                return info.getName();
            }
            return .getNameExt();
        }
        public String getDisplayName() {
            ProjectInformation info = .lookup(ProjectInformation.class);
            if (info != this) {
                return info.getDisplayName();
            }
            return getName();
        }
        public Icon getIcon() {
            ProjectInformation info = .lookup(ProjectInformation.class);
            if (info != this) {
                return info.getIcon();
            }
            return ImageUtilities.loadImageIcon(
                "org/netbeans/modules/ide/ergonomics/fod/project.png" // NOI18N
            , false);
        }
        public Project getProject() {
            return .lookup(Project.class);
        }
        public void addPropertyChangeListener(PropertyChangeListener listener) {
            .addPropertyChangeListener(listener);
        }
        public void removePropertyChangeListener(PropertyChangeListener listener) {
            .removePropertyChangeListener(listener);
        }
        final void associate(Project p) {
            if (p == null) {
                 = .;
                return;
            }
            assert .equals(p.getProjectDirectory());
            ProjectInformation info = p.getLookup().lookup(ProjectInformation.class);
            if (info != null) {
                for (PropertyChangeListener l : .getPropertyChangeListeners()) {
                    info.addPropertyChangeListener(l);
                }
            }
             = p.getLookup();
            for (ProjectOpenedHook h : p.getLookup().lookupAll(ProjectOpenedHook.class)) {
                .add(h);
            }
            List<RootNodelist = ;
             = Collections.emptyList();
            if (list != null) {
                for (RootNode fn : list) {
                    fn.change();
                }
            }
            .firePropertyChange(nullnullnull);
        }
        public Node createLogicalView() {
            LogicalViewProvider lvp = .lookup(LogicalViewProvider.class);
            if (lvp != null && lvp != this) {
                return lvp.createLogicalView();
            }
            if ( == null) {
                 = new ArrayList<RootNode>();
            }
            RootNode fn = new RootNode();
            .add(fn);
            return fn;
        }
        public Node findPath(Node rootObject target) {
            LogicalViewProvider lvp = .lookup(LogicalViewProvider.class);
            if (lvp != null && lvp != this) {
                return lvp.findPath(roottarget);
            }
            return null;
        }
    }
    private static final class RootNode extends FilterNode {
        public RootNode(FileObject fo) {
            super(DataFolder.findFolder(fo).getNodeDelegate());
        }
        public void change(Lookup l) {
            LogicalViewProvider lvp = l.lookup(LogicalViewProvider.class);
            if (lvp != null) {
                changeOriginal(lvp.createLogicalView(), true);
            }
        }
    }
New to GrepCode? Check out our FAQ X