blob: 84a5d344574b95a9dada8bdb653694c296fdb818 [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.selenium2.api;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.cookies.EditorCookie;
import org.openide.cookies.LineCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.text.Line;
import org.openide.util.Mutex;
/**
*
* @author Theofanis Oikonomou
*/
public final class Utils {
static final Logger LOGGER = Logger.getLogger(Utils.class.getName());
/**
* Opens the file and optionally set cursor to the line. This action is always run in AWT thread.
* @param file path of a file to open
* @param line line of a file to set cursor to, {@code -1} if no specific line is needed
*/
public static void openFile(File file, int line) {
openFile(file, line, -1);
}
/**
* Opens the file and optionally set cursor to the line and column. This action is always run in AWT thread.
* @param file path of a file to open
* @param line line of a file to set cursor to, {@code -1} if no specific line is needed
* @param column column within a line to set cursor to, {@code -1} if no specific column is needed
*/
public static void openFile(File file, int line, final int column) {
assert file != null;
FileObject fileObject = FileUtil.toFileObject(FileUtil.normalizeFile(file));
if (fileObject == null) {
LOGGER.log(Level.INFO, "FileObject not found for {0}", file);
return;
}
DataObject dataObject;
try {
dataObject = DataObject.find(fileObject);
} catch (DataObjectNotFoundException ex) {
LOGGER.log(Level.INFO, "DataObject not found for {0}", file);
return;
}
if (line == -1) {
// simply open file
EditorCookie ec = dataObject.getLookup().lookup(EditorCookie.class);
ec.open();
return;
}
// open at specific line
LineCookie lineCookie = dataObject.getLookup().lookup(LineCookie.class);
if (lineCookie == null) {
LOGGER.log(Level.INFO, "LineCookie not found for {0}", file);
return;
}
Line.Set lineSet = lineCookie.getLineSet();
try {
final Line currentLine = lineSet.getCurrent(line - 1);
Mutex.EVENT.readAccess(new Runnable() {
@Override
public void run() {
currentLine.show(Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FOCUS, column);
}
});
} catch (IndexOutOfBoundsException exc) {
LOGGER.log(Level.FINE, null, exc);
}
}
}