Tuesday, 3 September 2013

Generic collection of MenuItems in C#?

Generic collection of MenuItems in C#?

I am working on displaying a list of audio input devices in the menu, and
I'm fairly new to C#. The number of input devices not only varies from
machine to machine, it is possible that someone might add or subtract a
USB device while the program is running. I wrote code that checks whenever
the menu is activated, but I have to limit the number of possible input
devices. It's not likely that there will be more than 10 input devices,
but, in order to understand C# better, I'd like to see if it's possible to
use a generic so that I don't have to limit the list.
Here's what I have going for code now:
MainMenu sgFileMenu = new MainMenu();
List<MenuItem> inputDevice = new List<MenuItem>();
MenuItem myMenuItemInput = new MenuItem("&Input Devices");
sgFileMenu.MenuItems.Add(myMenuItemInput);
for (int i = 0; i < deviceCount; i++)
{
myMenuItemInput.MenuItems.Add(inputDevice[i]);
}
That compiles but gives an ArgumentOutOfRange exception when I run it. I'm
obviously missing something about how generics are set up- can somebody
clue me in?

null pointer exception - jxls, markAsFixedSizeCollection

null pointer exception - jxls, markAsFixedSizeCollection

I'm getting a null pointer exception when using the XLSTransformer's
markAsFixedSizeCollection method. Actually, the exception itself occurs at
net.sf.jxls.util.Util.duplicateRowCollectionProperty(Util.java:238).
Here's the code:
package com.mycomp;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import com.sun.media.sound.InvalidFormatException;
import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;
public class TestJXL {
public TestJXL() throws ParsePropertyException,
InvalidFormatException, IOException{
ArrayList<Employee> staff = new ArrayList<Employee>();
staff.add(new Employee("Derek", 35, 3000, 0.30));
staff.add(new Employee("Elsa", 28, 1500, 0.15));
staff.add(new Employee("Oleg", 32, 2300, 0.25));
staff.add(new Employee("Neil", 34, 2500, 0.00));
staff.add(new Employee("Maria", 34, 1700, 0.15));
staff.add(new Employee("John", 35, 2800, 0.20));
staff.add(new Employee("Leonid", 29, 1700, 0.20));
HashMap<String, ArrayList<Employee>> beans = new
HashMap<String, ArrayList<Employee>>();
beans.put("employee", staff);
XLSTransformer transformer = new XLSTransformer();
transformer.markAsFixedSizeCollection("employee");
String templateFileName = "C:\\0temp2\\test.xls";
String destFileName = "C:\\0temp2\\dest.xls";
try {
transformer.transformXLS(templateFileName, beans,
destFileName);
} catch
(org.apache.poi.openxml4j.exceptions.InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String [] args) throws
ParsePropertyException, InvalidFormatException, IOException{
new TestJXL();
}
}
package com.mycomp;
import java.util.Date;
public class Employee {
private String name;
private int age;
private Double payment;
private Double bonus;
private Date birthDate;
private Employee superior;
public Employee(String name, int age, Double payment, Double bonus) {
this.name = name;
this.age = age;
this.payment = payment;
this.bonus = bonus;
}
public Employee(String name, int age, double payment, double bonus, Date
birthDate) {
this.name = name;
this.age = age;
this.payment = new Double(payment);
this.bonus = new Double(bonus);
this.birthDate = birthDate;
}
public Employee(String name, int age, double payment, double bonus) {
this.name = name;
this.age = age;
this.payment = new Double(payment);
this.bonus = new Double(bonus);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Double getPayment() {
return payment;
}
public void setPayment(Double payment) {
this.payment = payment;
}
public Double getBonus() {
return bonus;
}
public void setBonus(Double bonus) {
this.bonus = bonus;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Employee getSuperior() {
return superior;
}
public void setSuperior(Employee superior) {
this.superior = superior;
}
}
Here's the stack trace:
Exception in thread "main" java.lang.NullPointerException
at net.sf.jxls.util.Util.duplicateRowCollectionProperty(Util.java:238)
at
net.sf.jxls.transformer.CollectionRowTransformer.processRowCollections(CollectionRowTransformer.java:116)
at
net.sf.jxls.transformer.CollectionRowTransformer.transform(CollectionRowTransformer.java:66)
at
net.sf.jxls.transformer.SheetTransformer.transformSheet(SheetTransformer.java:88)
at
net.sf.jxls.transformer.XLSTransformer.transformWorkbook(XLSTransformer.java:248)
at
net.sf.jxls.transformer.XLSTransformer.transformXLS(XLSTransformer.java:221)
at
net.sf.jxls.transformer.XLSTransformer.transformXLS(XLSTransformer.java:201)
at com.mycomp.TestJXL.<init>(TestJXL.java:32)
at com.mycompl.TestJXL.main(TestJXL.java:40)
The template and code is as suggested in:
http://jxls.sourceforge.net/reference/collections.html
Any suggestions appreciated.

twitter like url and htaccess

twitter like url and htaccess

Below is my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
RewriteRule ^(.*)/$ index.php?url=$1
</IfModule>
I am trying to do the fancy twitter url.
www.mywebsite.com/home rather than www.mywebsite.com/home.php
in my index.php code its like this:
<?php
$key=$_GET['url'];
if($key=='index')
{
include('welcome.php'); // welcome page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else
{
include('index.php'); // go back to index.php
}
?>
this keeps giving me a an invalid 404 error. and when I output: echo $key
it shows like this: index.php not index
what am i doing wrong?

How to prevent triggering mouseleave after mouseup?

How to prevent triggering mouseleave after mouseup?

In my javascript app I am having issues with event "propagation"(?),
namely I would like not to trigger mouseleave after mouse up on an element
( have implemented dragging behavior therefore it moves and mouse leaves
it).
How can I do that ?

DragTabFrame closing inconsistently

DragTabFrame closing inconsistently

The code below is supposed to work a little like the Multi-Document
Interface (MDI) you might see in a browser like FF, IE or Chrome. It
presents 'documents' (a black buffered image as spacer) in a tabbed pane
such that they can be dragged from the pane into a new (or existing)
window by user choice.
But it has had issues with closing frames once they have no more tabs, as
well as closing the JVM when there are no further visible windows. I think
I fixed them by checking with a Timer in the DragTabManager:
It checks open frames for instances of DragTabFrame
If it finds one, it check the tab count. If 0 the frame is set invisible
and disposed.
If it finds no instances of the frame that are visible, it ends the Timer
to allow the JRE to exit.
At least that is how it is supposed to work. It seems to be working
reliably here, and I have seen no 'empty frame or VM failing to shut down'
for quite a few tests. Does it work as advertised for others, or do I need
to look further?
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class DragTabFrame extends JFrame {
private JTabbedPane tabbedPane = new JTabbedPane();
private final static DragTabManager dragTabManager = new
DragTabManager();
final MouseAdapter ma = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
dragTabManager.setCurrentComponent(c);
DragTabFrame dtf = (DragTabFrame)c.getTopLevelAncestor();
dragTabManager.setCurrentFrame(dtf);
JTabbedPane tp = dtf.getTabbedPane();
int index = tp.indexOfComponent(c);
if (index<0) index = 0;
String title = tp.getTitleAt(index);
dragTabManager.setCurrentTitle(title);
}
@Override
public void mousePressed(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
DragTabFrame dtf = (DragTabFrame)c.getTopLevelAncestor();
dragTabManager.setCurrentComponent(c);
dragTabManager.setCurrentFrame(dtf);
JTabbedPane tp = dtf.getTabbedPane();
int index = tp.indexOfComponent(c);
if (index<0) index = 0;
String title = tp.getTitleAt(index);
dragTabManager.setCurrentTitle(title);
}
@Override
public void mouseReleased(MouseEvent e) {
JComponent c = (JComponent) e.getSource();
if
(c.getTopLevelAncestor().getBounds().contains(e.getLocationOnScreen()))
{
// do nothing, the drop point is the same frame
} else {
DragTabFrame dtf = getTargetFrame(e.getLocationOnScreen());
if (dtf == null) {
dtf = new DragTabFrame();
dtf.init();
dtf.setLocation(e.getLocationOnScreen());
} else {
DragTabFrame fromFrame =
dragTabManager.getCurrentFrame();
fromFrame.removeTabComponent(c);
JTabbedPane tp = fromFrame.getTabbedPane();
if (tp.getTabCount() == 0) {
fromFrame.setVisible(false);
fromFrame.dispose();
}
}
dtf.addTabComponent(dragTabManager.getCurrentTitle(), c);
dtf.pack();
dtf.setVisible(true);
}
}
};
public JTabbedPane getTabbedPane() {
return tabbedPane;
}
public DragTabFrame getTargetFrame(Point p) {
Frame[] frames = Frame.getFrames();
for (Frame frame : frames) {
if (frame instanceof DragTabFrame
&& frame.getBounds().contains(p)) {
return (DragTabFrame) frame;
}
}
return null;
}
public void init() {
// the GUI as seen by the user (without frame)
JPanel gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
gui.add(tabbedPane, BorderLayout.CENTER);
add(gui);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
setLocationByPlatform(true);
}
public void addTabComponent(String name, Component c) {
tabbedPane.addTab(name, c);
c.addMouseListener(ma);
c.addMouseMotionListener(ma);
}
public void removeTabComponent(Component c) {
tabbedPane.remove(c);
c.removeMouseListener(ma);
c.removeMouseMotionListener(ma);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
DragTabFrame dtf = new DragTabFrame();
dtf.init();
BufferedImage bi = new BufferedImage(
200, 40, BufferedImage.TYPE_INT_RGB);
for (int ii = 1; ii < 4; ii++) {
JLabel l = new JLabel(new ImageIcon(bi));
dtf.addTabComponent("Tab " + ii, l);
}
dtf.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
dtf.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
//
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
class DragTabManager {
private DragTabFrame currentFrame;
private JComponent currentComponent;
private String currentTitle;
private Timer timer;
public DragTabManager() {
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Frame[] frames = Frame.getFrames();
if (frames.length==0) {
timer.stop();
}
System.out.println("frames.length: " + frames.length);
boolean allInvisible = true;
for (Frame frame : frames) {
if (frame instanceof DragTabFrame) {
DragTabFrame dtf = (DragTabFrame)frame;
if (dtf.isVisible()) {
allInvisible = false;
}
if (dtf.getTabbedPane().getTabCount()==0) {
dtf.setVisible(false);
dtf.dispose();
}
}
}
if (allInvisible) {
timer.stop();
}
}
};
timer = new Timer(200,actionListener);
timer.start();
}
/**
* @return the currentFrame
*/
public DragTabFrame getCurrentFrame() {
return currentFrame;
}
/**
* @param currentFrame the currentFrame to set
*/
public void setCurrentFrame(DragTabFrame currentFrame) {
this.currentFrame = currentFrame;
}
/**
* @return the currentComponent
*/
public JComponent getCurrentComponent() {
return currentComponent;
}
/**
* @param currentComponent the currentComponent to set
*/
public void setCurrentComponent(JComponent currentComponent) {
this.currentComponent = currentComponent;
}
/**
* @return the currentTitle
*/
public String getCurrentTitle() {
return currentTitle;
}
/**
* @param currentTitle the currentTitle to set
*/
public void setCurrentTitle(String currentTitle) {
this.currentTitle = currentTitle;
}
}

Monday, 2 September 2013

Retrive Excel Sheet data into Sqlite Database Android

Retrive Excel Sheet data into Sqlite Database Android

In Android application ,i should get all the datas(nearly 500 coloums and
1000 rows) from Excel Sheet into Sqlite Database. For that i have used the
following code snippet,
FileInputStream is = new FileInputStream("Excel File Path in SDCard");
HSSFWorkbook wb = new HSSFWorkbook(is);
HSSFSheet sheet = wb.getSheet("Test");
Iterator<?> rowIterator = sheet.rowIterator();
int index = 0;
while (rowIterator.hasNext()){
HSSFRow hssfRow = (HSSFRow) rowIterator.next();
if(index++ != 0){
//In this stage i have storing each cell into database
util.inserrDataToSqliteDb(dbManager,
hssfRow.getCell(1).getStringCellValue(),
hssfRow.getCell(2).getStringCellValue()
}
}
When use this code it takes long time to get data from excel sheet. Taking
all the rows without any iteration into database may be a good idea. But i
could not get any reference to do that one.
Any suggestions ? Thanks,

NSException - no crash?

NSException - no crash?

I'm having a rather odd problem with my cocoa application. Whenever an
NSException is raised, the exception is printed to Console.app's log, but
doesn't cause the application to crash and the UI remains responsive. I do
not have an NSExceptionHandler installed and Xcode has created the app
bundle in "Release" mode.
What gives?