Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions java/java.editor/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@
<specification-version>1.7</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.spi.palette</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>1</release-version>
<specification-version>1.10</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.spi.tasklist</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JavaPalette/Items=Items
JavaPalette/Items/Item.xml=Item
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.java.editor.palette;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;

@ActionID(id = "org.netbeans.modules.html.palette.HtmlPaletteCustomizerAction", category = "Tools")
@ActionRegistration(iconInMenu = false, displayName = "#ACT_OpenJavaCustomizer")
@ActionReference(path = "Menu/Tools/PaletteManager", position = 200)
@Messages("ACT_OpenJavaCustomizer=&Java Code Clips")
public final class JavaPaletteCustomizerAction implements ActionListener {

public void actionPerformed(ActionEvent e) {
JavaSourceFileLayerPaletteFactory.createPalette().showCustomizer();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.java.editor.palette;

import java.io.IOException;
import javax.swing.Action;
import org.netbeans.spi.palette.DragAndDropHandler;
import org.netbeans.spi.palette.PaletteActions;
import org.netbeans.spi.palette.PaletteController;
import org.netbeans.spi.palette.PaletteFactory;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.datatransfer.ExTransferable;

/**
*
* @author geertjan
*/
public class JavaSourceFileLayerPaletteFactory {

public static final String JAVA_PALETTE_FOLDER = "JavaPalette";
private static PaletteController palette = null;

public JavaSourceFileLayerPaletteFactory() {
}

public static PaletteController createPalette() {
try {
if (null == palette)
palette = PaletteFactory.createPalette(
JAVA_PALETTE_FOLDER,
new MyActions(),
null,
new JavaDragAndDropHandler());
return palette;
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
return null;
}

private static class MyActions extends PaletteActions {

//Add new buttons to the Palette Manager here:
@Override
public Action[] getImportActions() {
return null;
}

//Add new contextual menu items to the palette here:
@Override
public Action[] getCustomPaletteActions() {
return null;
}

//Add new contextual menu items to the categories here:
@Override
public Action[] getCustomCategoryActions(Lookup arg0) {
return null;
}

//Add new contextual menu items to the items here:
@Override
public Action[] getCustomItemActions(Lookup arg0) {
return null;
}

//Define the default action here:
@Override
public Action getPreferredAction(Lookup arg0) {
return null;
}

}

private static class JavaDragAndDropHandler extends DragAndDropHandler {

public JavaDragAndDropHandler() {
super(true);
}

@Override
public void customize(ExTransferable t, Lookup item) {
//do nothing
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.java.editor.palette.items;

import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import org.netbeans.modules.java.editor.palette.items.resources.ItemCustomizer;
import org.openide.text.ActiveEditorDrop;

/**
*
* @author geertjan
*/
public class Item implements ActiveEditorDrop {

private String comment = "";

public Item() {
}

private String createBody() {
String comment = getComment();
StringBuilder buffer = new StringBuilder();
buffer.append( "/** " );
buffer.append( comment );
buffer.append( " */\n" );
buffer.append( "public static void main( String[] args ) {\n" );
buffer.append( "}\n\n" );
return buffer.toString();
}

@Override
public boolean handleTransfer(JTextComponent targetComponent) {

ItemCustomizer c = new ItemCustomizer(this, targetComponent);
boolean accept = c.showDialog();
if (accept) {
String body = createBody();
try {
JavaSourceFilePaletteUtilities.insert(body, targetComponent);
} catch (BadLocationException ble) {
accept = false;
}
}
return accept;

}

public String getComment() {
return comment;
}

public void setComment(String comment) {
this.comment = comment;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.java.editor.palette.items;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import javax.swing.text.BadLocationException;
import javax.swing.text.Caret;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.StyledDocument;
import org.openide.text.IndentEngine;
import org.openide.text.NbDocument;
import org.openide.util.Exceptions;

/**
*
* @author geertjan
*/
public class JavaSourceFilePaletteUtilities {

public static void insert(final String s, final JTextComponent target) throws BadLocationException {

final StyledDocument doc = (StyledDocument) target.getDocument();
if (doc == null) {
return;
}

class InsertFormatedText implements Runnable {
@Override
public void run() {
try {
insertFormated(s, target, doc);
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
}
InsertFormatedText insert = new InsertFormatedText();

//This starts the run() in the Runnable above:
NbDocument.runAtomicAsUser(doc, insert);

}

private static int insertFormated(String s, JTextComponent target, Document doc) throws BadLocationException {

int start = -1;

try {

//Find the location in the editor,
//and if it is a selection, remove it,
//to be replaced by the dropped item:
Caret caret = target.getCaret();
int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark());
doc.remove(p0, p1 - p0);

start = caret.getDot();

//Insert the string in the document,
//using the indentation engine
//to create the correct indentation:
IndentEngine engine = IndentEngine.find(doc);
StringWriter textWriter = new StringWriter();
try (Writer indentWriter = engine.createWriter(doc, start, textWriter)) {
indentWriter.write(s);
}

doc.insertString(start, textWriter.toString(), null);

} catch (IOException | BadLocationException ex) {
Exceptions.printStackTrace(ex);
}

return start;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
NAME_html-ITEM=Main Method
HINT_html-ITEM=\
<html>\
public static void main( String[] args ) {<br>\
}<br>\
</html>

LBL_Customizer_InsertPrefix=Insert
ItemCustomizer.commentField.text=
ItemCustomizer.commentLabel.text_1=Comment:
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
* 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.
*/.
-->
<!DOCTYPE editor_palette_item PUBLIC "-//NetBeans//Editor Palette Item 1.0//EN" "http://www.netbeans.org/dtds/editor-palette-item-1_0.dtd">

<editor_palette_item version="1.0">
<class name="org.netbeans.modules.java.editor.palette.items.Item" />

<icon16 urlvalue="org/netbeans/modules/java/editor/palette/items/resources/Item16.png" />
<icon32 urlvalue="org/netbeans/modules/java/editor/palette/items/resources/Item32.png" />
<description localizing-bundle="org/netbeans/modules/java/editor/palette/items/resources/Bundle"
display-name-key="NAME_html-ITEM"
tooltip-key="HINT_html-ITEM" />
</editor_palette_item>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading