blob: 669c95289100b4af58490e63db76e6499c06832e [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.maven.apisupport;
import java.awt.Image;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.openide.ErrorManager;
import org.openide.filesystems.*;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.util.RequestProcessor;
/**
* @author Milos Klent
*/
class AnnotatedNode extends AbstractNode implements Runnable, FileStatusListener {
private Set<FileObject> files;
private Set<FileStatusListener> fileSystemListeners;
private RequestProcessor.Task task;
private volatile boolean iconChange;
private volatile boolean nameChange;
private boolean forceAnnotation;
protected AnnotatedNode(Children children) {
super(children, null);
}
protected AnnotatedNode(Children children, Lookup lookup) {
super(children, lookup);
}
protected final void setFiles(final Set<FileObject> files) {
fileSystemListeners = new HashSet<FileStatusListener>();
this.files = files;
if (files == null) {
return;
}
Iterator<FileObject> it = files.iterator();
Set<FileSystem> hookedFileSystems = new HashSet<FileSystem>();
while (it.hasNext()) {
FileObject fo = it.next();
try {
FileSystem fs = fo.getFileSystem();
if (hookedFileSystems.contains(fs)) {
continue;
}
hookedFileSystems.add(fs);
FileStatusListener fsl = FileUtil.weakFileStatusListener(this, fs);
fs.addFileStatusListener(fsl);
fileSystemListeners.add(fsl);
} catch (FileStateInvalidException e) {
ErrorManager err = ErrorManager.getDefault();
err.annotate(e, "Cannot get " + fo + " filesystem, ignoring..."); // NOI18N
err.notify(ErrorManager.INFORMATIONAL, e);
}
}
}
protected final Set<FileObject> getFiles() {
return files;
}
protected void setForceAnnotation(boolean forceAnnotation) {
this.forceAnnotation = forceAnnotation;
}
protected final Image annotateIcon(final Image img, final int type) {
Image annotatedImg = img;
if (files != null && files.iterator().hasNext()) {
try {
FileObject fo = files.iterator().next();
annotatedImg = FileUIUtils.getImageDecorator(fo.getFileSystem()).
annotateIcon(img, type, files);
} catch (FileStateInvalidException e) {
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
}
}
return annotatedImg;
}
protected final String annotateName(final String name) {
String annotatedName = name;
if (files != null && files.iterator().hasNext()) {
try {
FileObject fo = files.iterator().next();
annotatedName = fo.getFileSystem().getDecorator().annotateName(name, files);
} catch (FileStateInvalidException e) {
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
}
}
return annotatedName;
}
@Override
public final void annotationChanged(FileStatusEvent event) {
if (task == null) {
task = RequestProcessor.getDefault().create(this);
}
boolean changed = false;
if (forceAnnotation || ((iconChange == false && event.isIconChange()) || (nameChange == false && event.isNameChange()))) {
Iterator<FileObject> it = files.iterator();
while (it.hasNext()) {
FileObject fo = it.next();
if (event.hasChanged(fo)) {
iconChange |= event.isIconChange();
nameChange |= event.isNameChange();
changed = true;
}
}
}
if (changed) {
task.schedule(50); // batch by 50 ms
}
}
@Override
public final void run() {
if (forceAnnotation || iconChange) {
fireIconChange();
fireOpenedIconChange();
iconChange = false;
}
if (forceAnnotation || nameChange) {
fireDisplayNameChange(null, null);
nameChange = false;
}
}
}