This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 151224 - Code formating improvment for code with mistakes
Summary: Code formating improvment for code with mistakes
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Source (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-10-23 21:55 UTC by cyb3r
Modified: 2010-09-08 06:54 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description cyb3r 2008-10-23 21:55:26 UTC
Almost always when I wan't to format copy&paste code, code formater will do mess in it or won't format at all.

Sample code:

class ColorTable extends JTable
{
boolean newitem[][];
ColorTable(int row, int col)
{
super(row,col);
newitem= new boolean[row][col];
}

public void setValueAt(Object aValue, int row, int column,boolean isnew)
{
super.setValueAt(aValue,row,column);
newitem[row][column]=isnew;
}

public void updateUI()
{
super.updateUI();
int columnCount = getColumnCount();
TableColumnModel columnModel= getColumnModel();
for(int i=0; i<columnCount; i++)
{
columnModel.getColumn(i).setCellRenderer(new ColorCellRenderer());
}
}
}
class ColorCellRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) 
{
super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
ColorTable ct=(ColorTable)table;
if(ct.newitem[row][column])
{
setBackground(Color.cyan);
}
else
{
setBackground(Color.white);
}
return this;
}
}




Result:

class ColorTable extends JTable {

    boolean newitem[][];

    ColorTable(int row, int col) {
        super(row, col);
        newitem = new boolean[row][col];
    }

    public void setValueAt(Object aValue, int row, int column, boolean isnew) {
        super.setValueAt(aValue, row, column);
        newitem[row][column] = isnew;
    }

    @Override
    public void updateUI() {
        super.updateUI();
        int columnCount = getColumnCount();
        TableColumnModel columnModel = getColumnModel();
        for (int i = 0; i & lt; columnCount;
        i++
        
            )
 
        
    


    {

     columnModel.getColumn(i)
            
         .setCellRenderer 
            (
          
            
        
         
    


    

        new ColorCellRenderer());
}
}
}

class ColorCellRenderer extends DefaultTableCellRenderer
{
    @Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) 
{
super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
ColorTable ct=(ResultsTable)table;
if(ct.newitem[row][column])
{
setBackground(Color.cyan);
}
else
{
setBackground(Color.white);
}
return this;
}
}


-==
What I've done?

1. Copy code from web page and paste to my project.
2. Select pasted code and use code formating from context menu.
Comment 1 Jiri Prox 2008-10-24 08:35:32 UTC
The code has wrong syntax - there is &lt; instead of < so the formatting which depends on syntax trees is broken.  

Maybe the formatting should not try to format incorrect code, but just indent it
Comment 2 cyb3r 2008-10-24 08:46:42 UTC
I've tried to format also good code (none errors were found by netbeans), with this same results. 
In my point of view if mistakes don't mess with code structure formatter should do his work.
Comment 3 Jiri Prox 2008-10-24 08:59:51 UTC
Can you please attach the correct code which is messed by formatter?
Comment 4 Petr Dvorak 2008-10-24 09:03:25 UTC
> I've tried to format also good code (none errors were found by netbeans), with this same results.

Which code did you use then? This is the essential information that can help us to deal with some bug in formatting...

I have fixed the &lt; character in the sample code and it worked well. I agree this could be done a little smarter.

Note: use of &lt; instead of <  messes with the code structure, as you suddenly replace token "relation operator" with a
token "identifier" and automaton analyzing the code have (understandably) problems with it... Note that if you use
unknown identifier in the code there an identifier should be, formatting works well...
Comment 5 cyb3r 2008-10-24 09:14:06 UTC
Ok, i've done this once again (with original source code without this &lt;) and it's work, but:

I've got two classes, when there is mistake, formatter dosn't work at all, but should at least format class that's correc.
The best thing will be if formatter won't format only metods that contains mistakes.

I've changed Issue type.
Comment 6 htmfilho 2010-09-08 06:54:58 UTC
To add one more detail to this formating issue, I have noticed that when adding the keyword "else" right after if's closing bracket, it goes to the beginning of the line when it is suppose to be below the closing bracket. See the situation below:

            @Override
            public void mousePressed(MouseEvent e) {
                if(segment && (e.getButton() == MouseEvent.BUTTON1)) {
                    startDrag = new Point(e.getX(), e.getY());
                    endDrag = startDrag;
                    repaint();
                }
 else
            }