Posts

Showing posts from 2014

Get System Information under JAVA

How to get the Computer brand and model from JAVA under Windows. private static String execCmd(String cmd) throws java.io.IOException { java.util.Scanner s = new java.util.Scanner(Runtime.getRuntime().exec(cmd).getInputStream()).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } public getSystemInfo() { try { String result=execCmd("wmic.exe computersystem get model,name,manufacturer,systemtype"); return result; } catch (IOException ex) { } return "Not available"; }

CORBA, OMNIORB, Asynchronous Method Invocation

AMI: Asynchronous Method Invocation under CORBA allows generation of asynchronous callback handlers and poolers. The idl compiler generates de corresponding code to handle the responses. 1- Create a 'idl' folder and create AMIExample.idl file into it: interface AMIExample {   string asyncMe(in string inputValue); }; 2- Create a compile script in the previous folder, for example idlcompile.bat: set OMINIDL=C:\omniorb\omniORB-4.2.0\bin\x86_win32\omniidl %OMINIDL% -bcxx -bami AmiExample.idl >> AmiExample.txt %OMINIDL% -bcxx -Wbami AmiExample.idl Parameter -bami AmiExample.idl >> AmiExample.txt indicates the compiler that it should create a description of the created interfaces and put it into AmiExample.txt. After executing the compiling script AmiExample.hh,AmiexampleSK.cc and AmiExample.txt will be created into the folder. AmiExample.txt gives a detailed definition of the available interfaces in the generated class. // ReplyHandler for interface AMIE

OmniOrb Snippets

Image
Obtaining the Implementation back from a Corba Object Assume that the implementation of a class named A is named AImp. A is-a Corba Object As soon as we encapsulate AImpl into an A_ptr in code such as (pseudocode): AImpl *aimp=new AImpl(orb); PortableServer::POA_var poa = PortableServer::POA::_narrow(root_poa); poa->activate_object(aimpl); CORBA::Object_var o; o = poa->servant_to_reference(aimpl); It seems as we have lost the implementation of A! Of course we have it present in the variable aimp, but as soon as we leave that method all we will be able to get is the remote interface of A as declared in its IDL. What if we have public methods in the declaration of A (AImpl.hh) that we want to keep public when locally to the object an private when remotely to it? There should be a way to recover the implementation when we are in the same process than it by using a dynamic_cast . Here it is (pseudocode): AImpl* aimp; corba::A_ptr ac= ... //obtain the corba ref

Pretty Print Large Texts in JAVA.

Image
Admit it, you have always wanted to impress your companion fellows adding impressive banners into your logs, it will seriously degrade performance but will fire your 'cool factor' up to the stars. For example:  public static void main(String[] args) { System.out.println(PrettyPrinter.prettyPrint("Big Enormous Rooster",200,25)); } Program output when told to print "Big Enormous Rooster" Here is the code of the method to acomplish your 'become popular' task: /** * Prints a text in a beautiful and pretty way */ public static String prettyPrint(String msg, int width, int height) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setFont(new Font("Trebuchet Ms", Font.PLAIN, height)); Graphics2D graphics = (Graphics2D) g; graphics.setRenderingHint(RenderingHints.KEY_TE

Vaadin 7: Detect Enter Key in a TextField

Vaadin 7: Detect Enter Key in a TextField This can be easily achieved just by adding a shortcutlistener whenever the TextField gets the focus and remove it when it blurs. From an IOC perspective wrapping the TextField would most probably result in less rework than extending the TextField: For example: TextField myTextField = new TextField("A text field"); myTextField.setImmediate(true); OnEnterKeyHandler onEnterHandler=new OnEnterKeyHandler(){             @Override             public void onEnterKeyPressed() {                  Notification.show("V oight Kampff Test ",                     Notification.Type.HUMANIZED_MESSAGE);             }         }; onEnterHandler.installOn(myTextField); ------------------------------------------------------------------- And the code for OnEnterKeyHandler is: import com.vaadin.event.FieldEvents; import com.vaadin.event.ShortcutAction; import com.vaadin.event.ShortcutListener; import com.vaadin.ui.TextField; p

Qt Signals and Slots, Connecting and Disconnecting

Image
Slots, slots everywhere... by Ramon Talavera Qt connects widgets by means of a nice designed scheme based on the idea that objectS may send signalS of different typeS to a single object instance: This is a screenshot of the example code running. The main application creates dialogs A and B and then connects the signal from A to the signal slot in B. I cannot believe I have clicked it 100 times to show you that it works... Declaring the signal:  A declares to be able to send signals of type ' somebodyClicked ': class ObjectADialog : public QDialog { Q_OBJECT public: explicit ObjectADialog(QWidget *parent = 0); ~ObjectADialog(); signals:      void somebodyClicked(QString who); protected: void closeEvent(QCloseEvent *event); private slots: void on_pushButton_clicked(); private: Ui::ObjectADialog *ui; }; Declaring the slot: B declares to contain a slot method for signals to connect to:   class ObjectBDialog : public QDialog

Generate a Finite Plane of a Given Size and Orientation Using its Normal Vector. OpenGL, Open Scene Graph.

*) Given a Plane with equation Ax+By+Cz+D=0 find two director vectors. *) Once obtained, vectors VD1 and VD2, find a third vector in the same plane VP that is perpendicular to VD1 plane ecuation: Ax+By+Cz+D=0, being (A,B,C) the director vector of the plane Getting two points in the plane: Given x=0, y=0 => Cz+D=0 -> Cz=-D/C ==> PointA=(0,0,-D/C) Given x=1, y=0 => A+Cz+D=0 -> Cz= -D-A -> z= (-D-A)/C   ==> PointB=(1,0,(-D-A)/C) Given x=0, y=1 => B+Cz+D=0 -> Cz= -D-B -> z= (-D-B)/C   ==> PointC=(0,1,(-D-B)/C) This was wrong, check code comments, solved there directorVectorOne= VectorFromPointAToPointB= (1,0, ((-D-A)/C)-(-D/C) ) = (1,0, (-D-A+D)/C ) = (1,0,-A/C) ) directorVectorTwo= VectorFromPointAToPointC= (0,1, ((-D-B)/C)-(-D/C) ) = (0,1, (-D-B+D)/C ) = (0,1, -B/C) ) TestVector_BC=(-1,1,(-D-B)/C-(-D-A)/C)) = (-1,1,(-B+A)/C) Example: 2x+y+8z-21=0 A=2 B=1 C=8  : Plane Normal Vector= (2