EditorFrame.java revision c23e82b612acd5e947c164114377578116f6d298
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Trond Norbye. All rights reserved.
* Use is subject to license terms.
*/
package org.opensolaris.opengrok.search.scope.editor;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.Reader;
/**
* The EditorFrame is just a simple frame used by the internal editor to display files
* @author Trond Norbye
*/
public class EditorFrame extends javax.swing.JFrame {
/**
* Creates new form EditorFrame
*/
public EditorFrame() {
initComponents();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension mySize = getPreferredSize();
setLocation(screenSize.width/2 - (mySize.width/2), screenSize.height/2 - (mySize.height/2));
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
textArea = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
textArea.setColumns(80);
textArea.setEditable(false);
textArea.setRows(25);
jScrollPane1.setViewportView(textArea);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 927, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
.addContainerGap()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 593, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* Load a named file into the JTextArea.
* @param filename The name of the file to display in the title of the frame
* @param reader A reader I may read data from
* @param lineno The line number to position the caret at (null if unknown)
* @throws java.io.IOException If an Exception occurs
*/
public void openFile(String filename, Reader reader, Integer lineno) throws IOException {
setTitle(filename);
StringBuilder sb = new StringBuilder();
char buffer[] = new char[8192];
int r;
while ((r = reader.read(buffer)) != -1) {
sb.append(buffer, 0, r);
}
textArea.setText(sb.toString());
int position = 0;
if (lineno != null) {
int line = lineno.intValue();
String text = textArea.getText();
for (int ii = 1; ii < line; ++ii) {
int p = text.indexOf("\n", position);
if (p == -1) {
break;
} else {
position = p + 1;
}
}
}
textArea.setCaretPosition(position);
textArea.setSelectionStart(position);
int end = textArea.getText().indexOf("\n", position);
if (end == -1) {
end = textArea.getText().length();
}
textArea.setSelectionEnd(end);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea textArea;
// End of variables declaration//GEN-END:variables
}