Upgraded to Netbeans 8.2, my minimap plugin can’t run

Upgraded to Netbeans 8.2, my minimap plugin can’t run, because it said i am using a wrong version of “Editor Lib 2”.

The STUPID fix to this problem is that i build the nbm, copy the “OpenIDE-Module-Module-Dependencies” from target/classes/META-INF/MANIFEST.MF , manually modify the version number, then pack it again. I don’t think this is a long term solution 🙁 so i won’t publish it to netbeans plugin central, i scare crashing somebody netbeans.

read count : 37

Netbeans api has bug, the west slidebar not always align to top

Netbeans api has bug, the west slidebar not always align to top, here my layout.xml

		<folder name="SideBar">
			<file name="com-pdk-sidebar-AddressSideBarFactory.instance">
				<attr name="position" intvalue="0"/>
				<attr name="location" stringvalue="West"/>
				<attr name="scrollable" boolvalue="true"/>

package com.pdk.sidebar;

import com.pdk.NetbeansUtil;
import com.pdk.ToggleAddressSideBarAction;
import com.peterdwarf.dwarf.Dwarf;
import com.peterdwarf.dwarf.DwarfLib;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.JComponent;
import javax.swing.text.JTextComponent;
import org.netbeans.api.project.Project;
import org.openide.loaders.DataObject;
import org.openide.util.Lookup;
import org.openide.util.Utilities;

public class AddressSideBarFactory implements org.netbeans.spi.editor.SideBarFactory {

	public static ArrayList<AddressPanel> addressPanels = new ArrayList<AddressPanel>();
	public static AddressPanel addressPanel;

	public JComponent createSideBar(JTextComponent jtc) {
		AddressSideBarFactory.addressPanel = new AddressPanel(jtc);
		int width = jtc.getFontMetrics(jtc.getFont()).stringWidth("00000000");
		addressPanel.setPreferredSize(new Dimension(width, 10));
		Project project = NetbeansUtil.getProject();
		if (project != null) {
			if (!project.getProjectDirectory().getName().toLowerCase().equals("peteri")) {
				return null;
			Lookup context = Utilities.actionsGlobalContext();
			DataObject file = context.lookup(DataObject.class);
			if (file != null) {
				//File dir = new File(file.getFolder().getPrimaryFile().getPath());

				File fileObj = new File(file.getPrimaryFile().getPath());
				String filename = fileObj.getName().toLowerCase();
				if (filename.endsWith(".c") || filename.endsWith(".cpp") || filename.endsWith(".cc") || filename.endsWith(".s") || filename.endsWith(".asm")) {
//					jtc.addCaretListener(new CaretListener() {
//						@Override
//						public void caretUpdate(CaretEvent e) {
//							addressPanel.repaint();
//						}
//					});

					jtc.addMouseListener(new MouseListener() {
						public void mouseClicked(MouseEvent e) {


						public void mousePressed(MouseEvent e) {

						public void mouseReleased(MouseEvent e) {


						public void mouseEntered(MouseEvent e) {


						public void mouseExited(MouseEvent e) {

					Vector<Dwarf> dwarfVector = DwarfLib.init(new File("/Users/peter/workspace/PeterI/kernel/kernel"), 0, false);
					Hashtable<Integer, BigInteger> ht = DwarfLib.getAddresses(dwarfVector, fileObj.getName());
					addressPanel.lineInfo = ht;
				} else {
					return null;


		return addressPanel;
package com.pdk.sidebar;

import com.pdk.ModuleLib;
import com.pdk.ToggleAddressSideBarAction;
import com.peterswing.CommonLib;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.math.BigInteger;
import java.util.Hashtable;
import javax.swing.JPanel;
import javax.swing.plaf.TextUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.Position;
import javax.swing.text.StyledDocument;
import javax.swing.text.View;
import org.netbeans.api.editor.mimelookup.MimeLookup;
import org.netbeans.api.editor.mimelookup.MimePath;
import org.netbeans.api.editor.settings.FontColorNames;
import org.netbeans.api.editor.settings.FontColorSettings;
import org.netbeans.editor.BaseTextUI;
import org.netbeans.editor.Coloring;
import org.netbeans.editor.Utilities;
import org.openide.text.NbDocument;

public class AddressPanel extends JPanel {

	private final JTextComponent jtc;
	Hashtable<Integer, BigInteger> lineInfo;
	TextUI textUI;
	View rootView;
	Color backgroundColor = Color.decode("#eaeaea");

	AddressPanel(JTextComponent jtc) {
		this.jtc = jtc;

		textUI = jtc.getUI();
		rootView = Utilities.getDocumentView(jtc);

	public void paint(final Graphics g) {
		if (!ToggleAddressSideBarAction.visible) {
//		ModuleLib.log(this.getParent());
//		ModuleLib.log(this.getParent().getParent());
		((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g.fillRect(0, 0, getWidth(), getHeight());

		Document document = jtc.getDocument();
		//HighlightsContainer hc = HighlightingManager.getInstance(jtc).getBottomHighlights();
		//HighlightsSequence hq = hc.getHighlights(0, document.getLength());

		FontMetrics metrics = g.getFontMetrics(jtc.getFont());
		int fontHeight = metrics.getHeight();

		int lineNo = document.getDefaultRootElement().getElementIndex(jtc.getCaretPosition());
		//ModuleLib.log("lineNo=" + lineNo + ", " + fontHeight);
		FontColorSettings fcs = MimeLookup.getLookup(MimePath.EMPTY).lookup(FontColorSettings.class);
		Coloring c = Coloring.fromAttributeSet(fcs.getFontColors(FontColorNames.CARET_ROW_COLORING));
		g.fillRect(0, fontHeight * lineNo, getWidth(), fontHeight);

		if (lineInfo != null) {
			try {
				if (rootView == null) {
				int startPos = getPosFromY(jtc, textUI, 0);
				int startViewIndex = rootView.getViewIndex(startPos, Position.Bias.Forward);
				int rootViewCount = rootView.getViewCount();

				if (startViewIndex >= 0 && startViewIndex < rootViewCount) {
					int y = 0;
					for (int i = startViewIndex; i < rootViewCount; i++) {
						View view = rootView.getView(i);
						if (view == null) {
						lineNo = NbDocument.findLineNumber((StyledDocument) document, view.getStartOffset());

						//g.drawString(String.valueOf(lineNo + 1), 0, (StyleConstants.getFontSize(as) + 5) * lineNo + 14);
						//int firstMargin = (fontHeight - StyleConstants.getFontSize(as)) / 2;
						BigInteger address = lineInfo.get(lineNo + 1);
						//ModuleLib.log(lineInfo + " = " + String.format("%08X", address));
						if (address != null) {
							g.drawString(String.format("%08X", address), 0, fontHeight * (y + 1) - 3);
						//ModuleLib.log("lineNo=" + lineNo);
			} catch (Exception ex) {

	private int getPosFromY(JTextComponent component, TextUI textUI, int y) throws BadLocationException {
		if (textUI instanceof BaseTextUI) {
			return ((BaseTextUI) textUI).getPosFromY(y);
		} else {
			// fallback to ( less otimized than ((BaseTextUI) textUI).getPosFromY(y) )
			return textUI.modelToView(component, textUI.viewToModel(component, new Point(0, y))).y;

read count : 25

RServe can get all variables from the ls() command

This is a working example in mac to get all variables from R’s ls() command using RServe

package com.mycompany.mmm;

import java.io.*;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.Rserve.RConnection;

 * helper class that consumes output of a process. In addition, it filter output of the REG command on Windows to look for InstallPath registry entry which specifies the location of R.
class StreamHog extends Thread {

	InputStream is;
	boolean capture;
	String installPath;

	StreamHog(InputStream is, boolean capture) {
		this.is = is;
		this.capture = capture;

	public String getInstallPath() {
		return installPath;

	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String line = null;
			while ((line = br.readLine()) != null) {
//				if (capture) { // we are supposed to capture the output from REG command
//					int i = line.indexOf("InstallPath");
//					if (i >= 0) {
//						String s = line.substring(i + 11).trim();
//						int j = s.indexOf("REG_SZ");
//						if (j >= 0) {
//							s = s.substring(j + 6).trim();
//						}
//						installPath = s;
//						System.out.println("R InstallPath = " + s);
//					}
//				} else {
//					System.out.println("Rserve>" + line);
//				}
				System.out.println("Rserve>" + line);
		} catch (IOException e) {

 * simple class that start Rserve locally if it's not running already - see mainly <code>checkLocalRserve</code> method. It spits out quite some debugging outout of the console, so feel free to modify
 * it for your application if desired.<p>
 * <i>Important:</i> All applications should shutdown every Rserve that they started! Never leave Rserve running if you started it after your application quits since it may pose a security risk.
 * Inform the user if you started an Rserve instance.
public class StartRserve {

	 * shortcut to <code>launchRserve(cmd, "--no-save --slave", "--no-save --slave", false)</code>
	public static boolean launchRserve(String cmd) {
		return launchRserve(cmd, "--no-save --slave", "--no-save --slave", false);

	 * attempt to start Rserve. Note: parameters are <b>not</b> quoted, so avoid using any quotes in arguments
	 * @param cmd command necessary to start R
	 * @param rargs arguments are are to be passed to R
	 * @param rsrvargs arguments to be passed to Rserve
	 * @return <code>true</code> if Rserve is running or was successfully started, <code>false</code> otherwise.
	public static boolean launchRserve(String cmd, String rargs, String rsrvargs, boolean debug) {
		try {
			Process p;
			boolean isWindows = false;
			String osname = System.getProperty("os.name");
			if (osname != null && osname.length() >= 7 && osname.substring(0, 7).equals("Windows")) {
				isWindows = true;
				/* Windows startup */
				p = Runtime.getRuntime().exec("\"" + cmd + "\" -e \"library(Rserve);Rserve(" + (debug ? "TRUE" : "FALSE") + ",args='" + rsrvargs + "')\" " + rargs);
			} else /* unix startup */ {
				p = Runtime.getRuntime().exec(new String[]{
					"/bin/sh", "-c",
					"echo 'library(Rserve);Rserve(" + (debug ? "TRUE" : "FALSE") + ",args=\"" + rsrvargs + "\")'|" + cmd + " " + rargs
//				p = Runtime.getRuntime().exec("/Users/peter/Library/R/3.4/library/Rserve/libs//Rserve --no-save --slave");
			System.out.println("waiting for Rserve to start ... (" + p + ")");
			// we need to fetch the output - some platforms will die if you don't ...
			StreamHog errorHog = new StreamHog(p.getErrorStream(), false);
			StreamHog outputHog = new StreamHog(p.getInputStream(), false);
			if (!isWindows) /* on Windows the process will never return, so we cannot wait */ {
			System.out.println("call terminated, let us try to connect ...");
		} catch (Exception x) {
			System.out.println("failed to start Rserve process with " + x.getMessage());
			return false;
		int attempts = 5;
		/* try up to 5 times before giving up. We can be conservative here, because at this point the process execution itself was successful and the start up is usually asynchronous */
		while (attempts > 0) {
			try {
				RConnection c = new RConnection();
				System.out.println("Rserve is running.");
				return true;
			} catch (Exception e2) {
				System.out.println("Try failed with: " + e2.getMessage());
			/* a safety sleep just in case the start up is delayed or asynchronous */
			try {
			} catch (InterruptedException ix) {
		return false;

	 * checks whether Rserve is running and if that's not the case it attempts to start it using the defaults for the platform where it is run on. This method is meant to be set-and-forget and cover
	 * most default setups. For special setups you may get more control over R with <<code>launchRserve</code> instead.
	public static boolean checkLocalRserve() {
		if (isRserveRunning()) {
			return true;
		String osname = System.getProperty("os.name");
		if (osname != null && osname.length() >= 7 && osname.substring(0, 7).equals("Windows")) {
			System.out.println("Windows: query registry to find where R is installed ...");
			String installPath = null;
			try {
				Process rp = Runtime.getRuntime().exec("reg query HKLM\\Software\\R-core\\R");
				StreamHog regHog = new StreamHog(rp.getInputStream(), true);
				installPath = regHog.getInstallPath();
			} catch (Exception rge) {
				System.out.println("ERROR: unable to run REG to find the location of R: " + rge);
				return false;
			if (installPath == null) {
				System.out.println("ERROR: canot find path to R. Make sure reg is available and R was installed with registry settings.");
				return false;
			return launchRserve(installPath + "\\bin\\R.exe");
		return launchRserve("/opt/local/Library/Frameworks/R.framework/Versions/3.4/Resources/bin/R");

	 * check whether Rserve is currently running (on local machine and default port).
	 * @return <code>true</code> if local Rserve instance is running, <code>false</code> otherwise
	public static boolean isRserveRunning() {
		try {
			RConnection c = new RConnection();
			System.out.println("Rserve is running.");
			return true;
		} catch (Exception e) {
			System.out.println("First connect try failed with: " + e.getMessage());
		return false;

	 * just a demo main method which starts Rserve and shuts it down again
	public static void main(String[] args) {
		System.out.println("checkLocalRserve()=" + checkLocalRserve());
		try {
			RConnection c = new RConnection();
			REXP x;
			x = c.eval("ls()");
			x = c.eval("a=c(1:10)");
			x = c.eval("bcd=rep(a,3)");
			x = c.eval("ls()");
			x = c.eval("R.version.string");
		} catch (Exception x) {

read count : 11

Netbeans R plugins with Rserve

Netbeans R plugins with Rserve. Rserve can interact with R using Java, huge ability to hock things in R. Heading to the first release of the R plugin. Anyone want to join the team, email me, Peter (mcheung63@hotmail.com).

Below are the first release features

  1. Syntax highlight, done, i think at least better than R-studio
  2. Function parameters popip, R-studio is totally useless at this point, it show up all R functions instead of the parameter list of the current function.
  3. R console, half done. I was using pipeline to create a R shell. Changing to us Rserve.
  4. Plot view. I am doing this
  5. Help/History window. Not start yet.
  6. Environment to see current defined variables. Not start yet.

The goals:

  1. Create a complete environment for people to run and debug R
  2. Add profiling feature, display performance in each line
  3. Compete with R-studio, get more users to use Netbeans. R-studio is simple and lots of places we can do better.
  4. Add support for Clouds. AWS/Azure
  5. Add support for frameworks, Hadoop/Spark
Syntax highlight is more nice than R-studio

read count : 354

Correct command to compile R 3.4.0 in Mac

Correct command to compile R 3.4.0 in Mac, i steal this commands from macports by “sudo port -d configure R”, so i know the default gfortran (/usr/local/bin/gfortran) won’t work, i have to use /opt/local/bin/gfortran-mp-6. Below is the minimal parameters set to build R.

LDFLAGS='-L/opt/local/lib -Wl,-headerpad_max_install_names' CPPFLAGS='-I/opt/local/include' CPATH='/opt/local/include' CXX='/usr/bin/clang++' CC='/usr/bin/clang' F77='/opt/local/bin/gfortran-mp-6' ../R-3.4.0/configure --prefix=/Users/peter/Desktop/install-r

read count : 16

Learning how to do raw logic programming

Learning how to do raw logic programming, convert the programming logic directly into logic gates. Found these two book in central library. The first one teach you everything you need to build up logic design. In undergrade year one, i remember uni was teaching us k-map and state diagram but without telling us why they are important and the use-case. I totally get messed and lost in space. Now I finally know how to use them. The verilog book  teach you how to convert programming looping into logic gates, but this book has too much text.

read count : 16

[Solved] Setup sharepoint 2016 standalone server configuration fail

Setup sharepoint 2016 standalone fail, the configuration wizard said “the specified user is a local account local accounts should only be used in standalone mode”, below command can fix it

New-SPConfigurationDatabase –DatabaseName SharePoint_Config –DatabaseServer <server> –AdministrationContentDatabaseName SharePoint_Content –Passphrase (ConvertTo-SecureString <password> –AsPlaintext –Force) –FarmCredentials (Get-Credential) -localserverrole SingleServerFarm

Make sure you has no error from the above command before you run the configuration wizard again, select “connect to farm” to finish the setup of sp2016

read count : 19

Explanation of DMIPS

There are essentially two aspects of CPU performance:
1) How many instructions can the CPU execute in a second.
2) How much actual WORK can the CPU do in one second.

The first is controlled by CPU architecture, memory speed, and so on. The second has those as variables, and add “how effective is the instruction set at doing the sort of work I want to do.” For instance, a PIC16xxx running at 20MHz executes 5 million instructions per second, but those instructions operate on 8bit data, and don’t include things like multiply and divide. But the instructions DO include a bunch of bit manipulation and IO instructions. So if you compare the pic to, oh, the 5MHz 8088 in the original IBM PC (should be safe an inoffensive), the PIC will be faster than the 8088 at some things (in particular those things having to do with touching external hardware), and the 8088 will be faster at other things (16bit math.)
For a long time, people tended to measure CPU performance with floating point benchmarks (fortran geeks they were, every one!) There was a set of benchmarks developed (1972!) called the Whetstone benchmarks that would measure a computers floating point performance. Eventually, people started using computers for things other than math, and realized they needed a similar benchmark for non-floating-point (integer) performance. This led to the Dhrystone benchmarks (get it?)

So the PIC32 runs at 80MHz, and generally executes instructions in about 1 cycle, so it does close to 80MIPs. It also runs the Dhrystone benchmark at about 1.5DMIPS/MHz (also 1.5DMIPs/MIP), which shows a pretty efficient architecture (at the sort of application the Dhrystone measures.) It’s more than a tiny core run at very high speed.

(This is one of the reasons you may have read the messages asking about pin-toggle speed. It’s pretty common for fast CPUs to incur a lot of overhead when they have to talk to the outside world; some of the ARM7TDMI toggle rates are pretty embarassing, and a 3GHz x86 cpu can’t do IO instructions much faster than the ancient 8088 (not to mention the terrible things such instructions do to the pipeline/etc.))

read count : 7